用于根据文本有条件地格式化(颜色填充)所选单元格的代码

时间:2019-06-13 20:28:28

标签: excel vba

我创建了这段代码来更改以字母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

已解决:

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

1 个答案:

答案 0 :(得分:0)

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