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.
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.
Can anyone suggest what I'm doing wrong?
Update the enum, and all of the places you've used Color. Blue will update. To get the long value of the RGB value to store, I just threw the value into the Immediate window and copied the output. The output will be 14390640.
The format of the RGB Value The format of an RGB value in the functional notation is 'rgb(' followed by a comma-separated list of three numerical values (three integer values(0-255, 0-255, 0-255)) followed by ')'.
First, choose the color you want changed in the browser. Then move the red, green, and blue sliders until you get the desired color. There are a few additional buttons to help you do this. The white button makes the color white (red = blue = green = 1.0) and the black color makes it black (red = blue = green = 0.0).
RGB values are used to specify colors. Red, green and blue are specified with values between 0 and 255.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With