简体   繁体   English

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

[英]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.ColorInterior.ColorIndex

Code of a Generic Sub 通用子代码

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.ColorIndexRange("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.

相关问题 从条件格式的单元格获取单元格颜色 - Get cell color from a conditionally formatted cell 如何找到已被条件格式化的单元格颜色 - How do I find the color of the cell which has been conditionally formatted 如何更改有条件格式的单元格的内部颜色索引 - How to change the interior colorindex of a conditionally formatted cell VBA function 测试单元格是否在 Excel 中有条件地格式化 - VBA function to test if cell is conditionally formatted in Excel 动态范围要根据单元格值进行条件格式化 - Dynamic range to be conditionally formatted based of cell value 检查条件格式化的单元格的颜色索引 - Checking color indexes of conditionally formatted cells 如果该行中的单元格已被条件格式化,则突出显示单元格或行 - Highlighting Cell or Row IF a cell in that row has been conditionally formatted 根据该行中的其他单元格(条件格式)颜色更改 1 个单元格的颜色 - Changing the colour of 1 cell based on other cells (conditionally formatted) colour in that row 按颜色评估条件格式单元格中的数据以返回列中的数据 - Evaluating data in Conditionally Formatted cells by color to return data in a column 突出显示 Excel 中的选定单元格而不清除单元格格式颜色 - Highlight Selected Cell in Excel without Clearing Cell Formatted Color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM