excel - 在 Excel 中使用条件格式宏跳过空格

标签 excel conditional-formatting vba

我使用了以下记录器宏:

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

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

最佳答案

您可以循环遍历所选内容中的每个单元格,并且仅在单元格不为空时应用格式。

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

关于excel - 在 Excel 中使用条件格式宏跳过空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371466/

相关文章:

vba - 无需复制按钮即可复制工作表

Excel VBA 2010 - 命令按钮停止使用选定的多个工作表

vba - Excel 未正确关闭 - 用户窗体挂起?

Excel其他选项卡多行多列VLOOKUP SUM

vba - 使用 Excel VBA 根据每个单元格中的值自动设置行格式?

Excel 条件格式将一行上的 3 色应用于多行

reporting-services - 如何根据 SSRS 中单独字段的值有条件地格式化整行的文本颜色?

vba - 如何使用 Worksheet.Function 在 VBA 中求负 SumIf

excel - 间接增加单元格引用 A1 A2 A3 等

sql - 编写联接的更好方法