vba 格式条件和内部颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12689750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:56:55  来源:igfitidea点击:

FormatConditions and Interior Color

excelexcel-vbaexcel-2010excel-2003vba

提问by assylias

I have an old macro that was working fine on Excel 2003 but creates issues with Excel 2010. The part that causes problems is:

我有一个旧的宏,它在 Excel 2003 上运行良好,但在 Excel 2010 上产生了问题。导致问题的部分是:

If Not IsNull(someRange.FormatConditions(parActiveCondition).Interior.Color) Then
    locVisibleColor = someRange.FormatConditions(parActiveCondition).Interior.Color
End if

where parActiveConditionis the active conditional formatting number on someRange.

parActiveCondition的活动条件格式编号在哪里someRange

When the background is selected as "No Color", someRange.FormatConditions(parActiveCondition).Interior.Colorreturns

当背景选择为“无颜色”时,someRange.FormatConditions(parActiveCondition).Interior.Color返回

  • Nullin Excel 2003
  • 0 in Excel 2010
  • Null在 Excel 2003 中
  • 0 在 Excel 2010 中

The problem is that a black background also returns 0. So in Excel 2010 it seems no longer possible to make the difference between a black background and no background color.

问题是黑色背景也返回 0。所以在 Excel 2010 中似乎不再可能区分黑色背景和无背景颜色。

Does anybody know a workaround?

有人知道解决方法吗?

ps: I could obviously select a white background instead of "No Color" but I'd rather not change all the spreadsheets and conditional formatting rules.

ps:我显然可以选择白色背景而不是“无颜色”,但我不想更改所有电子表格和条件格式规则。

回答by brettdj

You could use a Boolean secondary check such as

您可以使用布尔辅助检查,例如

IsNull(someRange.FormatConditions(parActiveCondition).Interior.ColorIndex) 'or
IsNull(someRange.FormatConditions(parActiveCondition).Interior.TintAndShade)

when

什么时候

.FormatConditions(parActiveCondition).Interior.Color = 0

回答by Trace

Option Explicit

Sub test()

Dim Color
Dim R As Integer
Dim G As Integer
Dim B As Integer

Color = ThisWorkbook.Sheets(1).Range("A1").Interior.Color


R = Color Mod 256
G = (Color \ 256) Mod 256
B = (Color \ 256 \ 256) Mod 256

ThisWorkbook.Sheets(1).Range("B1").Interior.Color = RGB(R, G, B)

End Sub

Black returns (0,0,0)
It appears however that "no color" returns (255,255,255) (=white)

黑色返回 (0,0,0)
然而,似乎“无颜色”返回 (255,255,255)(=白色)