vba 如何将RGB颜色存储在变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25201173/
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
How to store RGB colour in variable?
提问by Jonny
I'm looking to store an RGB colour in a variable in an Excel VBA project, to set the background color of various cell/ranges throughout a sub.
我希望将 RGB 颜色存储在 Excel VBA 项目中的变量中,以设置整个子中各种单元格/范围的背景颜色。
I want to set the colour once in a variable, so if I decide to change it throughout I only need to do it in one place.
我想在一个变量中设置一次颜色,所以如果我决定在整个过程中更改它,我只需要在一个地方进行。
Dim clrBlue As ColorFormat
clrBlue = RGB(0, 0, 256)
Range("a2").Interior.Color = clrBlue
Range("b3").Interior.Color = clrBlue
With the above code, I'm getting runtime error:
使用上面的代码,我收到运行时错误:
Object variable or With block variable not set
对象变量或未设置块变量
I could write separate functions  (SetBlue, SetRed, SetGreen) to apply each colour, but that feels messy.
我可以编写单独的函数 ( SetBlue, SetRed, SetGreen) 来应用每种颜色,但这感觉很混乱。
Can anyone suggest what I'm doing wrong?
谁能建议我做错了什么?
回答by Tom
RGBreturns a Long, so you need to declare clrBlueas Longinstead of as ColorFormat. 
RGB返回 a Long,因此您需要声明clrBlue为 asLong而不是 as ColorFormat。
Dim clrBlue As Long
clrBlue = RGB(0, 0, 255)
Application.union(Range("A2"), Range("B3")).Interior.Color = clrBlue
回答by Eric Harlan
As others have said, RGB() returns a Long, so you'll need to use that instead of ColorFormat. On a somewhat related note, I really like the Color enum in C#, and I started mimicking that in my VBA modules. You can create your own enum to store the values of colors in your project, then reference the color with Color.Blue.
正如其他人所说,RGB() 返回一个 Long,因此您需要使用它而不是 ColorFormat。有一点相关,我真的很喜欢 C# 中的 Color 枚举,我开始在我的 VBA 模块中模仿它。您可以创建自己的枚举来存储项目中的颜色值,然后使用 Color.Blue 引用颜色。
This also makes it really easy to modify a color, if you decide to go with a different shade of blue. Update the enum, and all of the places you've used Color.Blue will update.
如果您决定使用不同的蓝色阴影,这也使修改颜色变得非常容易。更新枚举,所有使用过 Color.Blue 的地方都会更新。
Example:
例子:
Public Enum Color
    Black = 0         'RGB(0, 0, 0)
    Blue = 14390640   'RGB(112, 149, 219)
    Gray = 11842740   'RGB(180, 180, 180)
    Red = 6118894     'RGB(238, 93, 93)
    White = 16777215  'RGB(255, 255, 255)
End Enum
To get the long value of the RGB value to store, I just threw the value into the Immediate window and copied the output.
为了获得要存储的 RGB 值的长值,我只是将值放入立即窗口并复制了输出。
In Immediate Window, type:
在立即窗口中,键入:
? RGB(112, 149, 219)
The output will be 14390640. There might be an easier way to get the value.
输出将是 14390640。可能有更简单的方法来获取该值。
回答by RIck_R
I haven't tried this and I'm not disputing any of the previous commenters.
我没有尝试过这个,我不反对任何以前的评论者。
I do notice that the original code sample has: clrBlue = RGB(0, 0, 256)
我注意到原始代码示例有: clrBlue = RGB(0, 0, 256)
The highest number allowed in RGB is 255. That might be the problem.
RGB 中允许的最高数字是 255。这可能是问题所在。

