gpt4 book ai didi

excel - 条件格式单元格的颜色索引

转载 作者:行者123 更新时间:2023-12-04 21:34:53 28 4
gpt4 key购买 nike

我有一张表,其中有一些条件格式的单元格。每个单元格有 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

因此,在您的情况下,您需要找出您要查看的条件格式规则。例如,假设我们要检查 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

关于excel - 条件格式单元格的颜色索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40826777/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com