简体   繁体   中英

Color index of conditionally formatted cell

I have a sheet where there are some conditionally formatted cells. There are 2 rules per cell for red and blue colors. There is another sheet where I have a If formula in the macro which checks for the color in those conditionally formatted cells:

If Range("Q10").End(xlDown).Interior.ColorIndex = 33 Then
code
End If

But it seems that this code won't work as the cells are conditionally formatted. The macro runs without entering the If formula and goes directly to End If. How do I ensure that it works?

Thanks

There is a way to get the Interior.Color or Interior.ColorIndex of a cell that was formatted with Conditional Formatting.

Code of a Generic Sub

Option Explicit

Sub GetFormatColor()

Dim CColor As Long
Dim CColorIndex As Long

' get the Color value of the first conditional formatting rule
CColor = Sheets("Sheet1").Range("Q10").FormatConditions(1).Interior.color

' get the ColorIndex value of the first conditional formatting rule
CColorIndex = Sheets("Sheet1").Range("Q10").FormatConditions(1).Interior.ColorIndex

End Sub

So, in your case, you need to find out which rule of the Conditional Formatting you are trying to look at. For example, let's say we want to check the Cell.ColorIndex of Range("Q10") , and the color is the first rule in the set of rules you have in your Conditonal Formatting.

Code sample for this post :

' modify "Sheet1" to your sheet's name, where you set-up the conditional formatting
If Sheets("Sheet1").Range("Q10").FormatConditions(1).Interior.ColorIndex = 33 Then
    MsgBox "ColorIndex is : " & Sheets("Sheet1").Range("Q10").FormatConditions(1).Interior.ColorIndex
End If

If you are using Excel 2010 (or later), you can use the DisplyFormat property of a range, so you can use the code below:

If Sheets("Sheet1").Range("Q10").DisplayFormat.Interior.ColorIndex = 33 Then
    MsgBox "ColorIndex is : " & Sheets("Sheet1").Range("Q10").DisplayFormat.Interior.ColorIndex
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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