【问题标题】:Have conditional formatting macro skip blanks in Excel让条件格式宏在 Excel 中跳过空白
【发布时间】:2013-06-28 18:38:21
【问题描述】:

我使用了以下记录器宏:

 Application.ScreenUpdating = False

    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=0", Formula2:="=19.5"
  Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
  With Selection.FormatConditions(1).Font
        .Bold = False
        .Italic = True
        .ColorIndex = 4

    End With
  Selection.FormatConditions(1).StopIfTrue = True

    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=19.6", Formula2:="=34.4"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
  With Selection.FormatConditions(1).Font
        .Bold = False
        .Italic = True
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.499984740745262
    End With
    Selection.FormatConditions(1).StopIfTrue = False

With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
Selection.FormatConditions(1).StopIfTrue = False

然后我使用宏来剪切所有条件,只保留格式。但是,无论我做什么,Isblank,添加另一个条件格式条件以仅在非空白上运行,在条件格式宏之后,格式为绿色(将任何 0-19.5 变为绿色,但单元格中没有任何内容)。

有没有办法给这个宏添加跳线?如果它是空白的,我希望它移动到下一个单元格。我没有设置范围,所以这就是全部选择的原因。

【问题讨论】:

    标签: excel conditional-formatting vba


    【解决方案1】:

    您可以遍历选择中的每个单元格,并且仅在单元格不为空白时应用格式。

    Option Explicit
    
    Sub test()
    
    Dim cel As Range
    
    Application.ScreenUpdating = False
    On Error Resume Next
    
    For Each cel In Selection
        If cel <> "" Then
    
        cel.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
            Formula1:="=0", Formula2:="=19.5"
      cel.FormatConditions(cel.FormatConditions.Count).SetFirstPriority
      With cel.FormatConditions(1).Font
            .Bold = False
            .Italic = True
            .ColorIndex = 4
    
        End With
      cel.FormatConditions(1).StopIfTrue = True
    
        cel.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
            Formula1:="=19.6", Formula2:="=34.4"
        cel.FormatConditions(cel.FormatConditions.Count).SetFirstPriority
      With cel.FormatConditions(1).Font
            .Bold = False
            .Italic = True
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.499984740745262
        End With
        cel.FormatConditions(1).StopIfTrue = False
    
    With cel
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
    cel.FormatConditions(1).StopIfTrue = False
    
    End If
    
    Next cel
    Application.ScreenUpdating = True
    End Sub
    

    【讨论】:

    • 嗯,我认为这可能有效,但是当我使用 find 将绿色的颜色格式替换为 ** 时,它会将空白单元格变成带有绿色格式的 **。同样,当我在该单元格中键入数字时使用宏来剪切格式后,它是绿色的。我认为我的条件格式宏(我不能在这里发布)在按顺序删除条件的同时保持格式,并且“默认”结果是绿色。如果我在单元格为空白的情况下添加一个条件来格式化单元格空白字体,这会有帮助吗?
    • 你的评论我看了五遍。我还是不明白你的意思。我发布的宏执行您在宏中的格式化操作。它只是不适用于空单元格。这就是问题,这就是答案。如果您还有其他问题,请提出新问题。
    • 很抱歉,如果不清楚。第 1 步:应用当前的上述宏。第 2 步:应用我的剪切条件,只保留格式化宏。第3步:使用查找并将所有低样本绿色格式替换为**。在这种情况下,所有空白单元格现在都包含一个 ** 而不是空白,这是我的问题。查找替换认为空白单元格中有绿色格式(这是第一个宏的结果)。
    • 您是否在文件上运行了一个宏,在空白单元格中设置了绿色 CF,然后然后您应用了我提供的宏?如果是这样,空白单元格将包含与第一个宏一起应用的 CF。您需要从这些单元格中删除 CF。您可以使用功能区命令从整个工作表中删除所有 CF 并使用正确的宏重新开始。
    • 不,我只是使用了你的宏,然后是我的第二个宏(它削减了条件格式)。当“条件被满足”时,一旦条件被“剪切”,空白单元格将被避免,空白单元格具有绿色格式。
    【解决方案2】:

    如果您只需要格式化,请使用您在此处记录的宏,并简单地复制导致格式化的代码。

     With Selection.FormatConditions(1).Font
            .Bold = False
            .Italic = True
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.499984740745262
     End With
    

    这段代码似乎有一些你可能想要的格式。

    此外,由于您使用的是选择而不是单独处理每个单元格,因此检查空白单元格会更加困难。据我所知,您不能这样做,因为您将选择视为一个整体。单元格也是一个范围。如果我错了,请有人纠正我。

    【讨论】:

    • 我需要将格式应用于另一组数据但没有条件,所以我不能只使用宏来格式化数据(因为它不正确)。我可以使用一个公式,但我的数据每次都不同大小或在同一个地方......
    【解决方案3】:

    我自己也遇到了类似的条件格式问题,单元格要么是空白要么有字符串值,我希望忽略前者。

    我发现条件格式函数不能与 ADDRESS() 一起正常工作,但我可以编写用户定义的函数以与 =AND() 一起使用,从而解决了这个问题。

    Public Function CellContent() As Variant
    
      On Error Resume Next
    
      CellContent = Application.Caller.value
    
    End Function
    
    Public Function Cell_HasContent() As Boolean
    
      On Error Resume Next
    
      If Application.Caller.value = "" Then
        Cell_HasContent = False
      Else
        Cell_HasContent = True
      End If
    
    End Function
    

    Resume Next 语句至关重要。如果您尝试检查任何非空白的值是否为 2,您现在可以使用以下命令进行检查:

    Formula1:="=AND(CellContent()=2,CellHasContent())"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多