基于IF的VBA颜色突出显示

所以我试图运行下面的代码。 它应该是直截了当的,我不明白为什么它不工作。 如果单元格b2是“约翰”,单元格O2是空的,那么O2应该是红色的。 如果O2不是空的,那么它不应该被着色。

任何帮助是必须赞赏。

Sub columnO(d As Long) If Cells(d, "B") = "John" And Cells(d, "O") = "" Then Cells(d, "O").Interior.Color = RGB(255, 0, 0) Else Cells(d, "O").Interior.Color = RGB(1000, 1000, 1000) End If End Sub Private Sub Worksheet_Change(ByVal Target As Range) If Not Application.Intersect(Range("B10:O10000"), Target) Is Nothing Then columnO Target.Row End If End Sub 

这可以很容易地使用条件格式。 只需从function区上的主菜单中select条件格式 – >新规则 – >使用公式来确定…并input公式

 =$B$2<>"John" 

那么你只需要设置你想要的格式。 您可以在公式中切换$,以便在需要时拖放和复制格式。

你为什么不使用条件格式而不是VBA? 可能更容易pipe理