Excel VBA RGB
In VBA everything boils down to coding of each and every piece, for an example if you want to reference some portion of the worksheet then we can use RANGE object, if you want to change the font color then we can make use of NAME property of the range then write font name that we needed but imagine a situation of changing the color of the font or background color of the cell for this we can make use of built-in VB colors like, vbGreen, vbBlue, vbRed, etc…But we have dedicated function to play around with different colors i.e. RGB function. In this article, we will give you a detailed explanation of the excel VBA RGB color function.
What is RGB & What Does it Do?
RGB stands for “Red, Green, and Blue” in this combination we can create different colors. Now the question of how we change colors i.e. by putting different integer numbers of RGB function we can change the colors.
Below is the syntax of the VBA RGB color function.
As you can see above we can supply three arguments i.e. Red, Green, and Blue. All these three parameters can accept integer numbers ranging from 0 to 255 only and the result of this function will be “Long” data type.
Examples of Excel RGB Function in VBA
Below are examples of excel RGB color function in VBA.
VBA RGB Example #1
For example, we have numbers from cell A1 to A8 as shown in the below image.
For this range of cells, we will try to change the font color to some random color by using VBA RGB color.
Start the macro procedure first.
Sub RGB_Example1() End Sub
First, we need to reference the range of cells of fonts we want to change the color of, in this case, our range of cells is A1 to A8, so supply the same by using RANGE object.
Sub RGB_Example1() Range ("A1:A8") End Sub
Put dot to see the IntelliSense list of RANGE object, form the IntelliSense list we are trying to changing the color of the font, and so choose FONT property from the list.
Sub RGB_Example1() Range("A1:A8").Font End Sub
Once the FONT property was chosen in this FONT property we are trying to change the color, so choose the color property of the FONT.
Sub RGB_Example1() Range("A1:A8").Font.Color End Sub
Put equal sign and open excel VBA RGB function.
Sub RGB_Example1() Range("A1:A8").Font.Color = RGB( End Sub
Give random integer numbers ranging from 0 to 255 for all the three arguments of the RGB function.
Sub RGB_Example1() Range("A1:A8").Font.Color = RGB(300, 300, 300) End Sub
Ok, now run the code and see the result of font colors of the cells from A1 to A8.
So, the colors of the font changed from black to some other. Color depends on the numbers we give to the VBA RGB function.
Below are VBA RGB color codes to get some of the common colors.
You can just change the integer number combination from 0 to 255 to get the different sorts of colors.
VBA RGB Example #2
For the same range of cells let’s see how to change the background color of these cells.
First, supply the range of cells by using the RANGE object.
Sub RGB_Example2() Range ("A1:A8"). End Sub
This time we are changing the background color of the mentioned cells, so we have nothing to do with FONT property now instead to change background color choose “Interior” property of the RANGE object.
Sub RGB_Example2() Range("A1:A8").Interior End Sub
Once the “Interior” property is selected put dot to see the properties and methods of this “Interior” property.
Sub RGB_Example2() Range("A1:A8").Interior. End Sub
Since we are changing the interior color of the mentioned cells choose “Color” property.
Sub RGB_Example2() Range("A1:A8").Interior.Color End Sub
To set the interior color property of the range of cells (A1 to A8) out the equal sign and open VBA RGB function.
Sub RGB_Example2() Range("A1:A8").Interior.Color = RGB( End Sub
Enter the random number as you want.
Sub RGB_Example2() Range("A1:A8").Interior.Color = RGB(0, 255, 255) End Sub
Run the code and see the background color.
The background color has been changed.
Things to Remember Here
- RGB stands for Red, Green, and Blue.
- A combination of these three colors will give different colors.
- All these three parameters can accept integer values between 0 to 255 only. Any numbers above this will be reset to 255.
This has been a guide to VBA RGB. Here we discuss how to change the RGB color of the interior cell (background, font) in excel VBA by putting different integer numbers along with examples and downloadable excel template. Below are some useful excel articles related to VBA-
- Example of Range Variable in Excel VBA
- How to Password Protect VBA Project?
- Compare Strings in VBA
- Excel VBA Web Scraping
- Square Root in VBA
- Color Index in VBA
- Font Color in VBA
- Custom Class in VBA
- How to Name Worksheet in VBA?