【发布时间】:2018-02-23 19:08:12
【问题描述】:
我正在尝试在 Excel 2013 的 VBA 中获得一些条件格式。如果列表验证为“完成”,则代码的执行应使 Col N 中的单元格(内部颜色)变为绿色,并且任何其他时间为白色。如果 Col N 中的列表验证为“保留”,则 Col O 中的单元格(内部颜色)应变为红色,其他时间为白色。
目前,出现的结果是:
1. 如果未从列表验证中选择任何内容,则 Col N 和 Col O 为白色。
2. 当从列表验证中选择任何内容时,Col N 变为绿色。
3. 在 Col N 中选择“保持”时,Col O 变为红色,如果在 Col N 中选择其他任何内容,则再次变为白色。
4. 如果在 Col O 中选择了某些内容,则单元格变为红色。
我当前的代码是(连同我已注释掉的部分):
'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
另外,有人可以解释何时使用 Operator:=xlNotEqual vs Operator:=xlEqual 吗?这些似乎与我的预期相反。
感谢您的帮助。
【问题讨论】: