2

我使用了以下记录器宏:

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

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

4

3 回答 3

2

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

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
于 2013-06-29T03:45:57.310 回答
0

如果格式化是您想要的全部,则使用您在此处记录的宏并简单地复制导致格式化的代码。

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

此代码块似乎具有您可能想要的一些格式。

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

于 2013-06-28T18:48:18.500 回答
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())"
于 2015-09-16T16:22:25.623 回答