Excel VBA:条件格式 – 内部颜色

我正在尝试在VBA for Excel 2013中使用一些条件格式。如果列表validation为“完成”,并且任何其他时间为白色,则代码的执行应将Col N中的单元格(内部颜色)变为“绿色”。 Col O中的单元格(内部颜色)应该在Col N中的列表validation为“已保留”时变为红色,而其他时间则为白色。

目前,出现的结果是:1.如果没有从列表validation中select,Col N和Col O是白色的。 2.列表validation中select了任何内容时,列N变为绿色。 3.当Col N中select“Held”时,Col O变为红色,如果select了Col N中的其他内容,Col O将再次变为白色。
4.如果在Col O中select了某项内容,则该单元格变为红色。

我现在的代码是(连同我已经注释过的部分):

'Add conditional format for column N. If Status is "Complete", color Status cell (col N) green (43). With Worksheets(SheetNum & " - Work").Range("N2:N2000").Select 'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=OR(($N2=""Not Started""),($N2=""In Queue""), ($N2=""In Work""), ($N2=""Held""), ($N2="" "")" 'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _ Formula1:="=($N2=""Complete"")" 'With Selection.FormatConditions(1) '.Interior.ColorIndex = 2 '.StopIfTrue = True 'End With 'End With Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _ Formula1:="=($N2=""Complete"")" With Selection.FormatConditions(1) .Interior.ColorIndex = 43 '.StopIfTrue = True End With End With 'Add conditional format for column O. If Status is "Held", color Held For cell (col O) 'red (3). 'With Worksheets(SheetNum & " - Work").Range("O2:O2000").Select 'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _ Formula1:="=OR(($N2=""Not Started""),($N2=""In Queue""), ($N2=""In Work""), ($N2=""Complete""))" 'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _ Formula1:="=($N2=""Held"")" 'With Selection.FormatConditions(1) '.Interior.ColorIndex = 2 '.StopIfTrue = True 'End With With Worksheets(SheetNum & " - Work").Range("O2:O2000").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _ Formula1:="=($N2=""Held"")" With Selection.FormatConditions(1) .Interior.ColorIndex = 3 '.StopIfTrue = True End With End With 

另外,有人可以解释什么时候使用运算符:= xlNotEqual vs运算符:= xlEqual? 这些似乎与我预期的相反。

谢谢你的帮助。

不幸的是,这些列需要在飞行中着色,这就是为什么我试图使用条件格式。 大约有10个条件格式,我试图将这些表格应用于程序的整个生命周期中。

在好的一面,我整天试了一下之后,发现我在(“”Held“”)之后错过了“)”。 但是,似乎没有使文件正常工作。

单元$ N2是否包含实际的单词“Complete”和“Held”?

如果是这样,也许这将工作

 Sub tester() For I = 1 To 100 ' or lastused row If InStr(1, UCase(Sheet6.Range("N" & I).Value), "COMPLETE") > 0 Then Sheet6.Range("N" & I).Interior.ColorIndex = 43 Else If InStr(1, UCase(Sheet6.Range("N" & I).Value), "HELD") > 0 Then Sheet6.Range("N" & I).Interior.ColorIndex = 3 Else Sheet6.Range("N" & I).Interior.ColorIndex = 2 End If End If Next I End Sub 

我终于找出正确的代码来得到我想要做的。

  'Add conditional format for column N. If Status is "Complete", color Status cell (col N) green (43). With Worksheets(SheetNum & " - Work").Range("N2:N2000").Select Selection.FormatConditions.Add Type:=xlExpression, _ Formula1:="=($N2=""Complete"")" With Selection.FormatConditions(1) .Interior.ColorIndex = 43 .StopIfTrue = True End With End With 

有人可以向我解释为什么这个工作,而不是在我的代码在第一篇文章中使用ColO相同的方法吗? 我想了解VBA代码,而不是将它们拼凑在一起。

感谢您的任何帮助。