【问题标题】:Conditional Formatting using Excel VBA code使用 Excel VBA 代码的条件格式
【发布时间】:2012-11-19 15:53:49
【问题描述】:

我有一个名为 DistinationRange 的 Range 对象,其中包含对范围 B3:H63 的引用

我想使用 Excel VBA 代码动态应用以下两个条件格式规则。 (因为范围不会一直相同)

  1. 如果单元格列 D 为空白,则不应应用任何格式(需要在此处使用 Stop If True)
  2. 如果 E 列单元格中的值小于 F 列单元格中的值,则该整行应具有绿色背景。

我尝试了很多使用录音,但录音不正确。

请帮助。

【问题讨论】:

    标签: vba excel conditional-formatting


    【解决方案1】:

    这将为您的简单案例提供答案,但您能否扩展您如何知道需要比较哪些列(在本例中为 B 和 C)以及初始范围(A1:D5 in这种情况)会怎样?然后我可以尝试提供更完整的答案。

    Sub setCondFormat()
        Range("B3").Select
        With Range("B3:H63")
            .FormatConditions.Add Type:=xlExpression, Formula1:= _
              "=IF($D3="""",FALSE,IF($F3>=$E3,TRUE,FALSE))"
            With .FormatConditions(.FormatConditions.Count)
                .SetFirstPriority
                With .Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 5287936
                    .TintAndShade = 0
                End With
            End With
        End With
    End Sub
    

    注意:这是在 Excel 2010 中测试的。

    编辑:基于 cmets 更新代码。

    【讨论】:

    • "A1:D5" 替换为"DestinationRange" 将理清初始范围的动态性质。
    • :-) 的确如此!虽然我也很好奇 B 和 C 列是静态的还是会随着DestinationRange
    • 如果首先将范围定义为A1D5,然后运行宏,然后在工作表中移动范围,然后看起来没问题,公式会动态更新。但是,如果首先将范围定义为不同的东西(例如L4O8),那么条件中的公式就会发生奇怪的事情。必须有一种方法可以使其适当地动态化。
    • @KevinPope:实际上我需要使用的范围和列是静态的。感谢您的回答,但应用的条件格式给出了不正确的结果。我需要使用的范围是“B3:H62”,我将条件格式添加为 Range("B3:H62").FormatConditions.Add Type:=xlExpression, Formula1:="=IF($D3="""",FALSE ,IF($F3>=$E3,TRUE,FALSE))" 此外,当我从 Excel 的前端界面查看格式条件时,它显示为 =IF($D2="",FALSE,IF($F2>=$ E2,真,假))
    • @SidHolland:你是对的。您是否能够找到解决此问题的任何方法?
    【解决方案2】:

    我想我刚刚发现了一种使用 VBA 以预期方式应用重叠条件的方法。 经过数小时尝试不同的方法后,我发现在创建每一个方法之后,改变条件格式规则的“适用于”范围是有效的!

    这是我的工作示例:

    Sub ResetFormatting()
    ' ----------------------------------------------------------------------------------------
    ' Written by..: Julius Getz Mørk
    ' Purpose.....: If conditional formatting ranges are broken it might cause a huge increase
    '               in duplicated formatting rules that in turn will significantly slow down
    '               the spreadsheet.
    '               This macro is designed to reset all formatting rules to default.
    ' ---------------------------------------------------------------------------------------- 
    
    On Error GoTo ErrHandler
    
    ' Make sure we are positioned in the correct sheet
    WS_PROMO.Select
    
    ' Disable Events
    Application.EnableEvents = False
    
    ' Delete all conditional formatting rules in sheet
    Cells.FormatConditions.Delete
    
    ' CREATE ALL THE CONDITIONAL FORMATTING RULES:
    
    ' (1) Make negative values red
    With Cells(1, 1).FormatConditions.add(xlCellValue, xlLess, "=0")
        .Font.Color = -16776961
        .StopIfTrue = False
    End With
    
    ' (2) Highlight defined good margin as green values
    With Cells(1, 1).FormatConditions.add(xlCellValue, xlGreater, "=CP_HIGH_MARGIN_DEFINITION")
        .Font.Color = -16744448
        .StopIfTrue = False
    End With
    
    ' (3) Make article strategy "D" red
    With Cells(1, 1).FormatConditions.add(xlCellValue, xlEqual, "=""D""")
        .Font.Bold = True
        .Font.Color = -16776961
        .StopIfTrue = False
    End With
    
    ' (4) Make article strategy "A" blue
    With Cells(1, 1).FormatConditions.add(xlCellValue, xlEqual, "=""A""")
        .Font.Bold = True
        .Font.Color = -10092544
        .StopIfTrue = False
    End With
    
    ' (5) Make article strategy "W" green
    With Cells(1, 1).FormatConditions.add(xlCellValue, xlEqual, "=""W""")
        .Font.Bold = True
        .Font.Color = -16744448
        .StopIfTrue = False
    End With
    
    ' (6) Show special cost in bold green font
    With Cells(1, 1).FormatConditions.add(xlCellValue, xlNotEqual, "=0")
        .Font.Bold = True
        .Font.Color = -16744448
        .StopIfTrue = False
    End With
    
    ' (7) Highlight duplicate heading names. There can be none.
    With Cells(1, 1).FormatConditions.AddUniqueValues
        .DupeUnique = xlDuplicate
        .Font.Color = -16383844
        .Interior.Color = 13551615
        .StopIfTrue = False
    End With
    
    ' (8) Make heading rows bold with yellow background
    With Cells(1, 1).FormatConditions.add(Type:=xlExpression, Formula1:="=IF($B8=""H"";TRUE;FALSE)")
        .Font.Bold = True
        .Interior.Color = 13434879
        .StopIfTrue = False
    End With
    
    ' Modify the "Applies To" ranges
    Cells.FormatConditions(1).ModifyAppliesToRange Range("O8:P507")
    Cells.FormatConditions(2).ModifyAppliesToRange Range("O8:O507")
    Cells.FormatConditions(3).ModifyAppliesToRange Range("B8:B507")
    Cells.FormatConditions(4).ModifyAppliesToRange Range("B8:B507")
    Cells.FormatConditions(5).ModifyAppliesToRange Range("B8:B507")
    Cells.FormatConditions(6).ModifyAppliesToRange Range("E8:E507")
    Cells.FormatConditions(7).ModifyAppliesToRange Range("A7:AE7")
    Cells.FormatConditions(8).ModifyAppliesToRange Range("B8:L507")
    
    
    ErrHandler:
    Application.EnableEvents = False
    
    End Sub
    

    【讨论】:

    • 您真的是要在错误处理部分再次将Application.EnableEvents 设置为False,还是打错字?
    猜你喜欢
    • 2011-10-02
    • 2016-10-02
    • 2017-11-03
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多