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

我已经使用logging器macros以下内容:

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 

然后,我使用一个macros来削减所有的条件,只留下格式。 然而,无论我做了什么,Isblank添加了另一个条件格式条件,只能在非空格上运行,在条件格式化macros之后,格式化为绿色(将任何0-19.5绿色变为绿色,但是单元格中没有任何内容)。

有没有办法给这个macros添加一个跳过行? 如果它是空白的,我希望它移动到下一个单元格。 我没有设定的范围,所以这就是为什么select。

您可以遍历所选内容中的每个单元格,并且只在单元格不为空的情况下应用该格式。

 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 

如果格式是你想要的,那么使用你在这里logging的macros,并简单地复制导致格式化的代码。

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

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

另外,因为你正在使用一个select来代替每个单元,所以检查一个空单元会更困难。 据我所知,你不能这样做,因为你把select作为一个整体来对待。 一个单元格也是一个范围。 有人纠正我,如果我错了。

我遇到了一个类似的问题,我自己用条件格式,单元格是空白的或者有一个string值,我希望忽略前者。

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