【问题标题】:Code to conditionally format (color fill) selected cells based on text基于文本有条件地格式化(颜色填充)选定单元格的代码
【发布时间】:2019-06-13 20:28:28
【问题描述】:

我创建了这段代码来更改以字母 S 结尾的单元格的字体,效果很好。我只需要更改它,而不是更改字体,而是用红色填充单元格。一个好处是如何有条件地格式化它以进行垂直的从左到右的颜色填充。无论如何,这是我用于更改字体颜色的代码:

Sub HighlightS()

    ' HighlightS Macro

    Dim rStart As Range
    Set rStart = Selection

    Selection.FormatConditions.Add Type:=xlTextString, String:="S", _

    TextOperator:=xlEndsWith

    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

    With Selection.FormatConditions(1).Font
        .Bold = True
        .Italic = False
        .Color = vbRed
        .TintAndShade = 0
    End With

    Selection.FormatConditions(1).StopIfTrue = False

    Selection.FormatConditions.Add Type:=xlTextString, String:="Not effective", _
                                   TextOperator:=xlContains

    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

    With Selection.FormatConditions(1).Font
        .Bold = True
        .Italic = False
        .Color = vbRed
        .TintAndShade = 0
    End With

    Selection.FormatConditions(1).StopIfTrue = False

End Sub

【问题讨论】:

  • 尝试使用宏记录器,它应该可以满足您的需求,或者至少是一个起点。
  • Selection.FormatConditions(1).Interior.Color = vbRed
  • 谢谢你们。我从未真正使用过宏记录器。这比手动找出代码要容易得多。另外,你说的对蒂姆有效。

标签: excel vba


【解决方案1】:

self-answered by Sabatino

Sub SFillRed()

    ' Highlights Macro
    Dim rStart As Range
    Set rStart = Selection

    Selection.FormatConditions.Add Type:=xlTextString, String:="S", _
                                   TextOperator:=xlEndsWith
    Selection.FormatConditions(1).Interior.Color = vbRed
    Selection.FormatConditions(1).StopIfTrue = False

    Selection.FormatConditions.Add Type:=xlTextString, String:="Not effective", _
                                   TextOperator:=xlContains
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).StopIfTrue = False

End Sub

【讨论】:

    猜你喜欢
    • 2014-12-31
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多