Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store RGB colour in variable?

Tags:

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?

like image 854
Jonny Avatar asked Aug 08 '14 10:08

Jonny


People also ask

How do you store RGB in a variable?

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.

How do you represent RGB values?

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 ')'.

How do I set RGB color?

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).

What is the range of RGB values?

RGB values are used to specify colors. Red, green and blue are specified with values between 0 and 255.


Video Answer


1 Answers

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 
like image 88
Tom Avatar answered Sep 16 '22 19:09

Tom