条件格式化单元格的颜色索引

我有一个工作表,有一些有条件格式化的单元格。 对于红色和蓝色,每个单元格有两条规则。 还有另一个工作表,我有一个在macros中的公式,检查那些有条件格式化的单元格中的颜色:

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

但似乎这个代码将不会工作,因为这些单元格是有条件格式化的。 macros运行时无需inputIf公式并直接进入End If。 我如何确保它的工作?

谢谢

有一种方法可以获取使用条件格式设置格式的单元格的Interior.ColorInterior.ColorIndex

通用子代码

 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 

所以,在你的情况下,你需要找出你想要看的条件格式的哪个规则。 例如,假设我们要检查Range("Q10")Cell.ColorIndex ,并且该颜色是您在条件格式化中的一组规则中的第一条规则。

此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 

如果您使用Excel 2010 (或更高版本),则可以使用范围的DisplyFormat属性,以便您可以使用下面的代码:

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