单元格的.Interior.ColorIndex值不同于什么是可见的
SO社区,
问题是我有一个通过条件格式化为红色的单元格,但是当我检查单元格的.Interior.ColorIndex
,它返回35
,它对应于浅绿色。
上下文信息
- 有问题的单元格是名为
partNumber_1
的合并单元格 ,它跨越单元格I12:L12 - 在即时窗口中,
?Range("I12").Interior.ColorIndex
返回35
-
?Range("partNumber_1").Interior.ColorIndex
返回35
- 如果将2.和3.更改为
.Interior.Color
(获取长.Interior.Color
值),则返回13434828
,其对应于RGB(205, 255, 205)
13434828
RGB(205, 255, 205)
- 有问题的命名范围
partNumber_1
属于另一个命名范围ScannedPartNumbers
,它是条件格式公式中引用的命名范围。 -
?Range("ScannedPartNumbers").Interior.ColorIndex
也返回35
我应用并通过VBA删除条件格式,其中target
是ScannedPartNumbers
范围, fillColor
是3(红色), fontColor
是6(黄色)。
Public Sub AddBlankCellFormatCondition(target As range, fillColor As Integer, Optional fontColor As Integer = 1) target.Parent.Unprotect password:=Strings.Mypw With target .FormatConditions.Add Type:=xlBlanksCondition .FormatConditions(.FormatConditions.Count).SetFirstPriority With .FormatConditions(1) .Interior.ColorIndex = fillColor .Font.ColorIndex = fontColor End With End With target.Parent.Protect password:=Strings.Mypw, userinterfaceonly:=True End Sub
ScannedPartNumbers
命名区域中的所有单元格都变成了红色,但是我还没有find具有.Interior.ColorIndex
或.Interior.Color
值的单个单元格,即使是远程类似于红色的!
请帮助?
将条件格式化看作放置在单元格格式顶部的透明表。 条件格式不会实际更改单元格的内部颜色,它会在顶部放置一个新的颜色,并强制Excel打印该颜色。
结果是单元格Color
和ColorIndex
值不会改变,所以VBA检查它们不会有任何区别。
我可以build议使用与条件格式相同的条件逻辑来检查要使用哪些单元格,而不是查看单元格的颜色。
在Excel中的颜色选项是严格意义上的显示目的,通常不应被视为值。 有很好的理由,您不能按颜色sorting或过滤。 虽然用VBA做类似的事情是可能的,但这并不是一个好主意,通常最好是鼓励用户使用值进行sorting和过滤,而不是使用颜色。
@Werrf告诉我一个单元格的条件格式化颜色与单元格的.Interior.ColorIndex
属性是完全分离的,我做了一些更多的search,发现这个CPearson文章 ,帮助我发现你需要先find所有单元格的哪一个条件格式条件是活动的,那么你可以轮询条件格式条件的.Interior.ColorIndex
属性。