简体   繁体   English

条件格式 Excel VBA

[英]Conditional Formatting Excel VBA

在此处输入图像描述

Hi, every day i have to update an excel file.嗨,我每天都必须更新 excel 文件。 This includes formatting column B. (see picture above).这包括格式化列 B。(见上图)。 I haven't found VBA code yet, to geht this kind of formatting via VBA.我还没有找到 VBA 代码,通过 VBA 进行这种格式化。 in the picture you see a subset of formatting rules, there are more.在图片中,您会看到格式化规则的子集,还有更多。 But there is only those three colors, which I have the hex code.但是只有那三个colors,我有十六进制代码。

yellow #9C5700黄色#9C5700

red #9C0006红色 #9C0006

green #006100绿色#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

I used this code, but i would like to use the hex colors.我使用了此代码,但我想使用十六进制 colors。 How do I use those?我该如何使用这些?

Per this Article :根据这篇文章

You can assign the color codes to any Color property of any object in either their decimal or hex representation.您可以将颜色代码以十进制或十六进制表示形式分配给任何 object 的任何 Color 属性。 Precede the Hex value with the &H prefix在十六进制值之前加上 &H 前缀

However for some reason VBA does swap the first two characters with the last two characters of a hex code, so your Yellow 9C7500 would go into VBA as 00759C但是由于某种原因,VBA 确实将前两个字符与十六进制代码的最后两个字符交换,因此您的黄色9C7500会将 go 转换为00759C

So, instead of .Interior.ColorIndex , use .Interior.Color and put in your hex codes with &H at the start.因此,不要使用.Interior.ColorIndex ,而是使用.Interior.Color并在开头使用&H输入十六进制代码。
Example:例子:

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

You can use .Color instead of .ColorIndex and you can also use RGB() to more easily set the value so change your code to您可以使用.Color而不是.ColorIndex ,也可以使用RGB()更轻松地设置值,因此将代码更改为

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

Please, try the next code.请尝试下一个代码。 Formatting the whole column will consume a lot of Excel resources, slows down the process of formulas update and it useless.格式化整列会消耗大量的Excel资源,减慢公式更新的过程,而且没用。 The above code format only the B:B column having data:上面的代码格式只有 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