【发布时间】:2017-03-05 16:45:38
【问题描述】:
我对 VBA 世界非常陌生,需要一些关于条件格式的 VBA 方面的帮助。
1) 我需要将条件格式应用于列 (M)
- 7 下绿色
- 7-20 的黄色
- 红色大于 20
如果列 (N) 声明 NOPO,我不希望应用条件格式。
我已经制定了一个公式来指示需要什么颜色,但无法将其转换为 VBA 条件格式(这个公式显示了什么颜色以及是否应该应用条件格式。
=IF(N2="osno",IF(M2<=7,"green",IF(M2<7,IF(M2>20,"red","less than 20"),IF(M2>20,IF(M2>20,"red","less than 20"),"yellow"))),"no format")
这是我当前的 VBA 脚本,毫无疑问,您可以看到它非常混乱,并且来自录制的脚本。
Sub Conditional()
'
' Notification_05 Macro
' Conditional Formatting
'
'
Sheets("Final").Select
Columns("M:M").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=8"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=8", Formula2:="=20"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=20"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 470000
.TintAndShade = 0
ActiveWindow.SmallScroll Down:=-27
Range("M2").Select
With Range("M:M")
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(M1))=0"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
End With
End With
End With
End Sub
谢谢,
布莱克
【问题讨论】:
-
您的
=IF(N2="osno",IF(M2<=7,"green",IF(M2<7,IF(M2>20,"red","less than 20"),IF(M2>20,IF(M2>20,"red","less than 20"),"yellow"))),"no format")等同于=IF(N2="osno",IF(M2<=7,"green",IF(M2>20,"red","yellow")),"no format"),因为M2<7部分将始终评估为False,第二个M2>20将始终为True,如果它超过第一个@ 987654329@。这并不能帮助您将其编写为 VBA 代码,但它可能会简化您认为需要做的事情。
标签: vba excel conditional-formatting multiple-conditions