【问题标题】:Conditional formatting VBA multiple conditions条件格式 VBA 多个条件
【发布时间】: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&lt;=7,"green",IF(M2&lt;7,IF(M2&gt;20,"red","less than 20"),IF(M2&gt;20,IF(M2&gt;20,"red","less than 20"),"yellow"))),"no format") 等同于=IF(N2="osno",IF(M2&lt;=7,"green",IF(M2&gt;20,"red","yellow")),"no format"),因为M2&lt;7 部分将始终评估为False,第二个M2&gt;20 将始终为True,如果它超过第一个@ 987654329@。这并不能帮助您将其编写为 VBA 代码,但它可能会简化您认为需要做的事情。

标签: vba excel conditional-formatting multiple-conditions


【解决方案1】:

CF 公式需要返回 true 或 false:您不能使用单个公式来分配多种颜色中的一种,而只能决定是否应应用颜色。您将需要三个规则,每个规则的公式略有不同。

Sub Tester()

    Dim rng As Range

    Set rng = Selection

    rng.FormatConditions.Delete 'clear any existing rules

    AddRule rng, "=AND(M2=""osno"", N2<7)", vbGreen
    AddRule rng, "=AND(M2=""osno"", N2>=7,N2<=20)", vbYellow
    AddRule rng, "=AND(M2=""osno"", N2>20)", vbRed

End Sub

'utility sub: add a CF rule given the formula and a fill color
Sub AddRule(rng, sFormula, lColor)
    With Selection.FormatConditions
        With .Add(Type:=xlExpression, Formula1:=sFormula)
            .Interior.Color = lColor
            .StopIfTrue = True
        End With
    End With
End Sub

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 2016-10-02
    • 1970-01-01
    • 2019-03-06
    • 2015-02-28
    • 1970-01-01
    • 2017-08-14
    • 2021-03-15
    • 2011-10-02
    相关资源
    最近更新 更多