use*_*266 1 macros excel vba conditional-formatting
我有一张工作表,其中有一些条件格式的单元格。对于红色和蓝色,每个单元格有 2 条规则。在另一个工作表中,我在宏中有一个 If 公式,用于检查这些条件格式单元格中的颜色:
If Range("Q10").End(xlDown).Interior.ColorIndex = 33 Then
code
End If
但由于单元格是有条件格式化的,因此该代码似乎不起作用。宏运行时无需输入 If 公式并直接转到 End If。我如何确保它有效?
谢谢
有一种方法可以获取使用条件格式设置的单元格的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
因此,就您的情况而言,您需要找出您要查看的条件格式规则。例如,假设我们要检查 of Cell.ColorIndex,Range("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
| 归档时间: | 
 | 
| 查看次数: | 8715 次 | 
| 最近记录: |