[英]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.
红细胞和蓝色每个细胞有2个规则。 There is another sheet where I have a If formula in the macro which checks for the color in those conditionally formatted cells:
还有另一张工作表,我在宏中有一个If公式,用于检查那些条件格式化单元格中的颜色:
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.
宏运行时不输入If公式,直接转到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. 有一种方法可以获取使用条件格式设置格式化的单元格的
Interior.Color
或Interior.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
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. 例如,假设我们要检查
Range("Q10")
Cell.ColorIndex
” Range("Q10")
,颜色是条件格式中规则集中的第一条规则。
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: 如果您使用的是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.