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

问题描述 投票:1回答:1

我已创建此代码来更改以字母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
excel vba
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.