如何使用 VBA 从 Excel 中的条件格式中获取背景颜色

问题描述:

我想在我的 VBA 脚本中获取使用 Excel 中的条件格式规则分配的单元格背景颜色.我意识到使用 Range.Interior.Color 属性没有从 Excel 应用条件格式功能产生的颜色.

I would like to obtain the cell background color assigned using a conditional formatting rule in Excel in my VBA script. I realized that using Range.Interior.Color property doesn't have the color resulting of an applied conditional formatting feature from Excel.

我做了一些研究,发现很长的路这里,它编译并运行但我没有得到指定的颜色 [我总是得到 (255,255,255)]

I did some research and I found this long way here, it compiles and runs but I don't get the assigned color [I get always (255,255,255)]

我使用的是 Excel 2016,我想知道是否有更简单的方法来使用一些内置的 VBA 函数或使用任何其他 excel 技巧来获取此信息.

I am using Excel 2016 and I am wondering if there is a simpler way to obtain this information using some built-in VBA function or using any other excel trick.

如果您想知道被条件格式规则 (CFR) 着色的单元格的颜色,请使用 Range.DisplayFormat.Interior.Color¹.

If you want to know the color of a cell that has been colored by a conditional formatting rule (CFR) then use Range.DisplayFormat.Interior.Color¹.

如果您想明确地知道一个单元格可能或可能没有被 CFR 着色的颜色,您需要遍历可能影响该单元格的 CFR 并查看每个 Range.FormatConditions(*x*).Interior.Color.

If you want to definitively know what color a cell may or may not have been colored by a CFR you need to iterate through the CFRs that could be affecting that cell and look at each of the Range.FormatConditions(*x*).Interior.Color.

文档:范围.显示格式

¹ 注意:.DisplayFormat 不适用于工作表 UDF.

¹ Note: .DisplayFormat is not available for a worksheet UDF.