【发布时间】:2018-08-20 15:04:03
【问题描述】:
如果 B 列中的单元格小于 F 列中的值,我需要突出显示该单元格。例如,如果单元格 B2 为 10,F2 为 20,则 B2 应为红色。但是在 B 列中有空白单元格,我不希望这些单元格突出显示。例如 B6 为空白,但 F6 为 10。在我的代码中,B6 也变为红色。
另外,我将如何突出显示同一行中已经突出显示的单元格。例如,如果 B2 突出显示,则突出显示 F2。
我的代码如下:
Sub threecf()
Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition
Set rg = Range("B2", Range("B2").End(xlDown))
'clear any existing conditional formatting
rg.FormatConditions.Delete
'define the rule for each conditional format
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlLess, "=f2")
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=isempty(f2)")
'define the format applied for each conditional format
With cond1
.Interior.Color = vbRed
.Font.Color = vbWhite
End With
With cond2
.Interior.Color = vbWhite
.Font.Color = vbWhite
End With
End Sub
【问题讨论】:
-
为什么是 VBA 而不是 Excel 公式?
-
如果你的范围不是太大,也许你应该直接用VBA命令格式化你的单元格,而不是使用条件格式。