繁体   English   中英

条件格式 Excel VBA

[英]Conditional Formatting Excel VBA

在此处输入图像描述

嗨,我每天都必须更新 excel 文件。 这包括格式化列 B。(见上图)。 我还没有找到 VBA 代码,通过 VBA 进行这种格式化。 在图片中,您会看到格式化规则的子集,还有更多。 但是只有那三个colors,我有十六进制代码。

黄色#9C5700

红色 #9C0006

绿色#006100

' (1) Highlight defined good as green values
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "=2")
    .Interior.ColorIndex = 6
    .StopIfTrue = False
End With

With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "in Anfrage")
    .Interior.ColorIndex = 6
    .StopIfTrue = False
End With



' (2) Highlight defined ok as yellow values
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "=1")
    .Interior.ColorIndex = 4
    .StopIfTrue = False
End With

With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "ok")
    .Interior.ColorIndex = 4
    .StopIfTrue = False
End With


' (2) Highlight defined bad as red values
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "=3")
    .Interior.ColorIndex = 3
    .StopIfTrue = False
End With

With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "kritisch")
    .Interior.ColorIndex = 3
    .StopIfTrue = False
End With

End Sub

我使用了此代码,但我想使用十六进制 colors。 我该如何使用这些?

根据这篇文章

您可以将颜色代码以十进制或十六进制表示形式分配给任何 object 的任何 Color 属性。 在十六进制值之前加上 &H 前缀

但是由于某种原因,VBA 确实将前两个字符与十六进制代码的最后两个字符交换,因此您的黄色9C7500会将 go 转换为00759C

因此,不要使用.Interior.ColorIndex ,而是使用.Interior.Color并在开头使用&H输入十六进制代码。
例子:

' (1) Highlight defined good as green values
With Range("b:b").FormatConditions.Add(xlCellValue, xlEqual, "=2")
    .Interior.Color = &H006100
    .StopIfTrue = False
End With

您可以使用.Color而不是.ColorIndex ,也可以使用RGB()更轻松地设置值,因此将代码更改为

.Interior.Color = RGB(&H9C,&H57,&H00)

请尝试下一个代码。 格式化整列会消耗大量的Excel资源,减慢公式更新的过程,而且没用。 上面的代码格式只有 B:B 列有数据:

Sub SetFormatRngMultiple_Cond()
 Dim ws As Worksheet, lastR As Long, rngF As Range

 Set ws = ActiveSheet
 lastR = ws.Range("B" & ws.rows.count).End(xlUp).row
 Set rngF = ws.Range("B2:B" & lastR)
 With rngF
        'first condition:
        With .FormatConditions
                .Delete
                .Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=2"
        End With
        With .FormatConditions(.FormatConditions.count)
                .Font.Color = 1137094
                .Interior.Color = vbYellow
                .SetFirstPriority: .StopIfTrue = False
        End With
        
        'second condition:
         With .FormatConditions
                .Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=1"
        End With
        With .FormatConditions(.FormatConditions.count)
                .Font.Color = 5287936
                .Interior.Color = 11854022
                .StopIfTrue = False
        End With
        
        'third condition:
         With .FormatConditions
                .Add Type:=xlTextString, String:="OK", TextOperator:=xlContains
        End With
        With .FormatConditions(.FormatConditions.count)
                .Font.Color = 5287936
                .Interior.Color = 11854022
                .StopIfTrue = False
        End With
        
        'fourth condition:
         With .FormatConditions
                .Add Type:=xlTextString, String:="kritish", TextOperator:=xlContains
        End With
        With .FormatConditions(.FormatConditions.count)
                .Font.Color = vbRed
                .Interior.Color = 14083324
                .StopIfTrue = False
        End With
 End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM