在Excel中具有条件格式宏跳过空白

时间:2013-06-28 18:38:21

标签: excel excel-vba conditional-formatting vba

我使用了Recorder Macro以下内容:

 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绿色,但单元格中没有任何内容)。

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果格式化是您想要的全部,那么使用您在此处记录的宏,只需复制导致格式化的代码。

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

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

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

答案 2 :(得分:0)

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

我发现条件格式化函数无法与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())"