繁体   English   中英

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

[英]Color index of conditionally formatted cell

我有一张表格,其中有一些有条件格式的单元格。 红细胞和蓝色每个细胞有2个规则。 还有另一张工作表,我在宏中有一个If公式,用于检查那些条件格式化单元格中的颜色:

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

但似乎这些代码不起作用,因为单元格是有条件格式化的。 宏运行时不输入If公式,直接转到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.ColorIndexRange("Q10") ,颜色是条件格式中规则集中的第一条规则。

此帖子的代码示例

' 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

暂无
暂无

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

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