首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >条件格式化VBA多个条件

条件格式化VBA多个条件
EN

Stack Overflow用户
提问于 2016-10-24 01:00:14
回答 1查看 8.2K关注 0票数 2

我对VBA世界非常陌生,需要一些条件格式化的VBA方面的帮助。

1)我需要将条件格式应用于列(M)

  • 绿色7岁以下
  • 黄色7-20
  • 红大于20

如果列(N)声明NOPO,则使用重写条件,我不希望应用条件格式。

我已经制定了一个公式来指示需要什么颜色,但不能将其转换为VBA条件格式(这个公式显示了什么颜色,以及是否应该应用条件格式。

代码语言:javascript
运行
复制
=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脚本,毫无疑问,您可以看到它非常混乱,并且是从一个录制的脚本中得到的。

代码语言:javascript
运行
复制
    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

谢谢,

布莱克

EN

回答 1

Stack Overflow用户

发布于 2016-10-24 03:40:15

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

代码语言:javascript
运行
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40209398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档