How to store RGB colour in variable?

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)

The highest number allowed in RGB is 255. That might be the problem.


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.

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.

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.

In Immediate Window, type:

? RGB(112, 149, 219)

The output will be 14390640. There might be an easier way to get the value.


RGB returns a Long, so you need to declare clrBlue as Long instead of as ColorFormat.

Dim clrBlue As Long

clrBlue = RGB(0, 0, 255)

Application.union(Range("A2"), Range("B3")).Interior.Color = clrBlue

Tags:

Colors

Excel

Vba