VBA 根据 1 列的值更改 3 列字体颜色的代码(多个表)
VBA code to change font color for 3 columns based on the value of 1 column (multiple tables)
情况:
我在一个 .xlsx 工作簿中有大约 2,600 个 table,每个选项卡一个 table。它们将作为 PDF 在线发布,但首先,我需要抑制频率小于 10 的行中的频率和百分比。掩码格式不适用于 SAS 9.3 中的交叉列表选项。所以,我认为最有效的方法是将相应单元格中的字体颜色更改为白色。不幸的是,当您 select 多个标签时,条件格式不起作用。使用 VBA 似乎是最好的选择,但我对此知之甚少。
这是 table 的示例(为简洁起见,我隐藏了 4-7 年级的行):
Example: Original Table
目标:
将具有计算值的单元格中的字体颜色更改为白色以模仿抑制。例如:
Example: "Suppressed" Table
谁能给我指出正确的方向?我觉得这应该很简单,但每次我这么想时,情况恰恰相反。我看到了一些类似的问题,但 none 的答案看起来与我的问题相关。
非常感谢!!!!
它们总是在“C
”列中吗?如果没有,你也必须检查那种东西。
它总是从第 10 行开始吗?
就循环遍历工作表和值而言,如果你想要简单,这就是最简单的:
Sub whiteout()
Dim c As Range, ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
For Each c In Range(ws.Range("C10"), ws.Range("C65000").End(xlUp))
If c.Value < 10 Then c.Font.Color = vbWhite
Next c
Next ws
End Sub
For each
循环非常直观,但请注意,在循环中访问和更改此类内容在大规模上可能会非常慢。
杰医生
我基于您的数据集制作了我自己的虚拟 table。
我会使用这样的宏:
Sub clean_lower_than_10()
Dim Sheet As Worksheet
For Each Sheet In ActiveWorkbook.Sheets
For Each Row In Sheet.UsedRange.Rows
'Columns where the frequency and percentage are: C,D -> 3,4
For Each Cell In Row.Cells
'Inside this condition, you should change the numbers for the column number that corresponds to the ones on your tables.
If Cell.Column = 3 Or Cell.Column = 4 Then
If Cell.Value < 10 Then
Cell.Font.ColorIndex = 2
End If
End If
Next
Next
Next
End Sub
colorIndex = 2 表示单元格字体为白色。
此解决方案假定您的百分比和频率值始终位于每个 sheet 的同一列中。
如果不是这种情况,您将不得不编写代码以使其工作,但它认为这将是一个很好的起点。
执行宏前我的table:
执行宏后我的table:
请注意,该值保持不变,但字体颜色已更改为白色。
希望对您有所帮助
回答有关为导出创建临时副本的附带问题:
Sub CreateExportCopy()
Dim wb As Workbook, wbExport As Workbook, newPath As String
Set wb = Workbooks("DataTables.xlsx") '<< your (open) data file
newPath = wb.Path & "\ForPDF_" & wb.Name
'this will create a copy of the workbook in the same location,
' with the name prepended by "ForPFDF_"
wb.SaveCopyAs newPath
'open the copy
Set wbExport = Workbooks.Open(newPath)
'follow one of the other answers here to process the
' tables in wbExport, but use (eg)
' c.ClearContents
' instead of
' c.Font.Color = vbWhite
End Sub
情况:
我在一个 .xlsx 工作簿中有大约 2,600 个 table,每个选项卡一个 table。它们将作为 PDF 在线发布,但首先,我需要抑制频率小于 10 的行中的频率和百分比。掩码格式不适用于 SAS 9.3 中的交叉列表选项。所以,我认为最有效的方法是将相应单元格中的字体颜色更改为白色。不幸的是,当您 select 多个标签时,条件格式不起作用。使用 VBA 似乎是最好的选择,但我对此知之甚少。
这是 table 的示例(为简洁起见,我隐藏了 4-7 年级的行):
Example: Original Table
目标:
将具有计算值的单元格中的字体颜色更改为白色以模仿抑制。例如:
Example: "Suppressed" Table
谁能给我指出正确的方向?我觉得这应该很简单,但每次我这么想时,情况恰恰相反。我看到了一些类似的问题,但 none 的答案看起来与我的问题相关。
非常感谢!!!!
它们总是在“C
”列中吗?如果没有,你也必须检查那种东西。
它总是从第 10 行开始吗?
就循环遍历工作表和值而言,如果你想要简单,这就是最简单的:
Sub whiteout()
Dim c As Range, ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
For Each c In Range(ws.Range("C10"), ws.Range("C65000").End(xlUp))
If c.Value < 10 Then c.Font.Color = vbWhite
Next c
Next ws
End Sub
For each
循环非常直观,但请注意,在循环中访问和更改此类内容在大规模上可能会非常慢。
杰医生
我基于您的数据集制作了我自己的虚拟 table。
我会使用这样的宏:
Sub clean_lower_than_10()
Dim Sheet As Worksheet
For Each Sheet In ActiveWorkbook.Sheets
For Each Row In Sheet.UsedRange.Rows
'Columns where the frequency and percentage are: C,D -> 3,4
For Each Cell In Row.Cells
'Inside this condition, you should change the numbers for the column number that corresponds to the ones on your tables.
If Cell.Column = 3 Or Cell.Column = 4 Then
If Cell.Value < 10 Then
Cell.Font.ColorIndex = 2
End If
End If
Next
Next
Next
End Sub
colorIndex = 2 表示单元格字体为白色。
此解决方案假定您的百分比和频率值始终位于每个 sheet 的同一列中。
如果不是这种情况,您将不得不编写代码以使其工作,但它认为这将是一个很好的起点。
执行宏前我的table:
执行宏后我的table:
请注意,该值保持不变,但字体颜色已更改为白色。
希望对您有所帮助
回答有关为导出创建临时副本的附带问题:
Sub CreateExportCopy()
Dim wb As Workbook, wbExport As Workbook, newPath As String
Set wb = Workbooks("DataTables.xlsx") '<< your (open) data file
newPath = wb.Path & "\ForPDF_" & wb.Name
'this will create a copy of the workbook in the same location,
' with the name prepended by "ForPFDF_"
wb.SaveCopyAs newPath
'open the copy
Set wbExport = Workbooks.Open(newPath)
'follow one of the other answers here to process the
' tables in wbExport, but use (eg)
' c.ClearContents
' instead of
' c.Font.Color = vbWhite
End Sub