首页 文章

VBA Excel:将条件格式应用于BLANK单元格

提问于
浏览
1

我正在尝试编写一个子过程,它将一些条件格式应用于Excel中的一系列单元格 . 我有点卡住所以我使用了宏录音机 . 然而,我无法弄清楚为什么它应用下面的公式,当我手动运行代码时,它失败了 .

  • 我想要做的是将条件格式应用于范围中的空白单元格 .

  • 我想让细胞颜色变灰

  • 范围是一个表,该表名为'Table1' .

  • 我需要在sub中执行此操作,因为表会动态刷新 .

下面是录制的宏不起作用,而是将格式应用于错误的单元格 . 任何纠正它的帮助将不胜感激

谢谢

Sub MacroTest()

    Range("Table1").Select
    'The below formula is wrong but I can't figure out what it should be
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=LEN(TRIM(D15))=0"
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = 0.399945066682943
    End With
    Selection.FormatConditions(1).StopIfTrue = False

End Sub

3 回答

  • 4

    试试这个( Tried and tested

    更改

    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=LEN(TRIM(D15))=0"
    

    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=LEN(TRIM(C" & Range("Table1").Row & "))=0"
    

    所以你的代码可以写成

    Sub Sample()
        With ThisWorkbook.Sheets("Sheet1").Range("Table1")
            .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=LEN(TRIM(C" & .Row & "))=0"
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            With .FormatConditions(1).Interior
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorDark2
                .TintAndShade = -9.99481185338908E-02
            End With
        End With
    End Sub
    
  • 4

    这是我的看法,即使希德已经有了一个很好的答案 . 我重新创建了一个名为 test 的表,并将其定位在 A1 . 我使用了一些代码的小编辑,它对我来说很好 .

    Sub Test()
    Dim v As Range
    Set v = Range("test")
    v.ClearFormats
    v.FormatConditions.Add Type:=xlExpression, Formula1:="=LEN(TRIM(A1))=0"
    With v.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent2
            .TintAndShade = 0.399945066682943
    End With
    v.FormatConditions(1).StopIfTrue = False
    End Sub
    

    然而,正如注释一样,在公式中使用 A2 会产生不灵活的结果,特别是与上面的代码中使用的Sid相比 .

    希望它有所帮助(或者至少提供一些见解)!

    SECOND TAKE:

    从前几天这一直困扰着我,所以我会再给它一次 . 显然,基于这个Microsoft Support nugget,CF似乎存在问题 . 存在两种解决方法:通过使用绝对引用或在应用CF之前首先选择单元格 .

    我玩了很多次,并且绝对参考了很多次错误的结果 . 但是,一种简单的方法可行 . 我们选择 Table1 的第一个单元格并给它CF,然后我们使用书中最简单的方法:格式画家!我们还用 .FormatConditions.Delete 替换了 .ClearFormats .

    以下是使用上述方法的代码变体:

    Sub Test()
    Dim Table1 As Range: Set Table1 = ThisWorkbook.Sheets("Sheet1").Range("Table1")
        Start = Timer()
        Application.ScreenUpdating = False
        Table1.FormatConditions.Delete
        With Table1.Cells(2, 1)
        'With Range("B7")
            .Select
            .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=LEN(TRIM(B7))=0"
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            With .FormatConditions(1).Interior
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorAccent2
                    .TintAndShade = 0.399945066682943
            End With
            .FormatConditions(1).StopIfTrue = False
            .Copy
        End With
        Table1.PasteSpecial xlPasteFormats 'or the next one
        'Range("B7:AO99").PasteSpecial xlPasteFormats
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        Debug.Print Timer() - Start
    End Sub
    

    这是结果的预览 .

    Sample Table1

    执行时间(以秒为单位)是:

    • 4.296875E-02

    • 4.492188E-02

    • 5.273438E-02

    • 5.859375E-02

    • 0.0625

    这些比我以前的尝试要快得多,我在所有细胞中循环并向每个细胞添加CF.

    希望这对你有所帮助!

  • 0

    经过一些搜索后,我找到了一个不使用LEN函数并且需要使用xlBlanksCondition指定范围的选项 . 我不知道为什么宏录制器会提供LEN解决方案,如果它也可以使用xlBlanksCondition解决方案 .

    资料来源:MSDN Microsoft

    我首先选择一个范围,然后我应用此代码:

    With Selection.FormatConditions.Add(Type:=xlBlanksCondition)
        .StopIfTrue = False
        .Interior.PatternColorIndex = xlAutomatic
        .Interior.Color = RGB(226, 80, 80)
        .Interior.ThemeColor = xlThemeColorAccent2
        .Interior.TintAndShade = 0.39
        .Font.Color = vbBlack
        .Font.TintAndShade = 0
        .Borders.LineStyle = xlContinuous
        .Borders.TintAndShade = 0
        .Borders.Weight = xlThin
        .Borders.Color = RGB(255, 0, 0)
        .StopIfTrue = False
    End With
    

相关问题