excel 整张工作表的条件格式

5jvtdoz2  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(225)

我正在尝试将任何符合预设值的储存格转换成色彩。
如果单元格值为:

  • S-DAYS、C-DAYS、DAYS将使单元格变为蓝色,文本为黑色
  • E SWING、S-E SWING、C-E SWING,它将变为绿色并带有黑色文本
  • L SWING、S-L SWING、C-L SWING,它将变为浅紫并带有黑色文本
  • LATES、S-LATES、C-LATES将变为灰色,文本为黑色
  • AOT将变为黄色,文本为黑色
  • VAC、OUT、MIL、TRAIN,它将使单元格变黑,文本为白色

我记录了以下内容。如何让它自动应用到工作表而不需要提示?

Sub DAConditionalFormating()
'
' DAConditionalFormating Macro
'

'
    Range("D14:XFD999").Select

    Selection.FormatConditions.Add Type:=xlTextString, String:="DAYS", _
        TextOperator:=xlContains
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlTextString, String:="E SWING", _
        TextOperator:=xlContains
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlTextString, String:="LATES", _
        TextOperator:=xlContains
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlTextString, String:="VAC", _
        TextOperator:=xlContains
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
7hiiyaii

7hiiyaii1#

将条件格式应用于整个工作表是一个非常非常糟糕的主意。每次编辑工作表中的任何单元格时,计算都会启动。
不要对整个工作表设置条件格式,而是使用代码只设置刚更改的单元格的格式。设置单元格填充和字体的格式。
在工作表模块的Worksheet_Change事件中执行此程式码,并让它在目的储存格上运作。然后它会快速执行,并只变更刚刚变更的储存格。
类似于这样。根据需要添加更多的ElseIF。代码进入工作表的Sheet模块。

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Value = "foo" Then
    With Target.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 15773696
        .TintAndShade = 0
    End With
ElseIf Target.Value = "bar" Then
    With Target.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
    End With
End If

End Sub

相关问题