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

在Excel中使用条件格式宏跳过空格,excel,conditional-formatting,vba,Excel,Conditional Formatting,Vba,我已使用Recorder宏执行以下操作: Application.ScreenUpdating = False Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _ Formula1:="=0", Formula2:="=19.5" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPr

我已使用Recorder宏执行以下操作:

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


是否有方法将跳过行添加到此宏?如果它是空的,我希望它移动到下一个单元格。我没有设置范围,所以这就是为什么它都在选择中。

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

 With Selection.FormatConditions(1).Font
        .Bold = False
        .Italic = True
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.499984740745262
 End With
这段代码似乎有一些您可能需要的格式


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

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

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
选项显式
子测试()
暗淡的cel As范围
Application.ScreenUpdating=False
出错时继续下一步
对于选择中的每个cel
如果cel“”那么
cel.FormatConditions.Add类型:=xlCellValue,运算符:=xlBetween_
公式1:=“=0”,公式2:=”=19.5”
cel.FormatConditions(cel.FormatConditions.Count).SetFirstPriority
使用cel.FormatConditions(1).Font
.Bold=错误
.Italic=True
.ColorIndex=4
以
cel.FormatConditions(1).StopIfTrue=True
cel.FormatConditions.Add类型:=xlCellValue,运算符:=xlBetween_
公式1:=“=19.6”,公式2:=”=34.4”
cel.FormatConditions(cel.FormatConditions.Count).SetFirstPriority
使用cel.FormatConditions(1).Font
.Bold=错误
.Italic=True
.ThemeColor=xlThemeColorDark1
.TintAndShade=-0.499984740745262
以
cel.FormatConditions(1).StopIfTrue=False
和cel
.HorizontalAlignment=xlCenter
.垂直对齐=xl底部
.WrapText=False
.方向=0
.AddIndent=False
.1级别=0
.ShrinkToFit=False
.ReadingOrder=xlContext
.MergeCells=False
以
cel.FormatConditions(1).StopIfTrue=False
如果结束
下一个细胞
Application.ScreenUpdating=True
端接头

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

我发现条件格式函数不能正确地与ADDRESS()配合使用,但我可以编写用户定义的函数与=和()配合使用,这就解决了这个问题

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
接下来的陈述至关重要。如果您试图检查任何非空格的值是否为2,现在可以使用以下选项进行检查:

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

嗯,我想这可能行得通,但当我使用find将绿色的颜色格式替换为**时,它会将空白单元格转换为带有绿色格式的**。另外,当我在单元格中键入数字时,使用宏剪切格式后,该单元格为绿色。我认为我的条件格式宏(我不能在这里发布)在按顺序删除条件的同时保持格式,并且“默认”结果为绿色。如果我添加一个条件来设置单元格空白字体的格式,如果单元格是空白的,那会有帮助吗?我已经读了你的评论五遍了。我还是不明白你的意思。我发布的宏执行宏中的格式化操作。它只是不适用于空单元格。这就是问题所在,这就是答案。如果您有其他问题,请提出新问题。如果不清楚,请道歉。步骤1:应用当前的上述宏。第2步:应用我的剪切条件,只保留格式化宏。步骤3:使用“查找”并将所有低示例绿色格式替换为**。在这种情况下,所有空白单元格现在都包含**,而不是空白,这是我的问题。“查找替换”认为空白单元格中有绿色格式(这是第一个宏的结果)。您是否在文件中运行宏,在空白单元格中设置绿色CF,然后应用我提供的宏?如果是这样,空白单元格将包含与第一宏应用的CF。您需要从这些单元格中删除CF。您可以使用Ribbon命令从整个工作表中删除所有CF,然后使用正确的宏重新开始。不,我只是使用了您的宏,然后是我的第二个宏(它剪切条件格式)。当条件“被满足”时,空白被避免,一旦条件被“切割”,空白单元格具有绿色格式化。我需要将格式化应用到不同的数据集上,但是没有条件,所以我不能只使用宏来格式化数据(因为它不正确)。我可以使用一个公式,但我的数据不是相同的大小,也不是每次都在同一个地方。。。