Excel VBA Color Index

In VBA, we have two ways of applying the color. The first uses the “Color” property, and the second uses the “ColorIndex” property.

We apply color to the cell as background, font, and border colors. So to access the color and color index property, we must first select the cell.

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Color Index (wallstreetmojo.com)

How to use Color & Color Index Property?

#1 – Using Color Property

Assume you have the word “Hello” in cell A1. First, we will see how to apply the background color of this cell.

To change the background color in color properties, we need first to mention the range of the cell.

Code:

Sub Color()

Range (“A1”)

End Sub

After selecting the cell, we need to mention what we need to do. As we said, we need to change the interior color of the cell. So to change the background color of the cell, use the property “Interior.”

Sub Color()

Range(“A1”).Interior

End Sub

Under the “Interior” property, we have several other methods and properties. Since we need to change the color of the cell, use the color property.

Sub Color()

Range(“A1”).Interior.Color =

End Sub

Here, we can use eight constantly named colors. Below is the list of the same.

Select the color type as per your wish.

Sub Color()

Range(“A1”).Interior.Color = vbBlue

End Sub

If we run this code using the F5 key or manually, it will change the background color of cell A1 to value.

Like this, you can use other constant named colors. Try using them to test the result.

Apart from these eight constant colors, we can also fill them with different colors by using the RGB function in VBARGB Function In VBARGB can be also termed as red green and blue, this function is used to get the numerical value of the color value, this function has three components as a named range and they are red, blue and green the other colors are considered as the components of these three different colors in VBA.read more.

Sub Color()

Range(“A1”).Interior.Color = RGB(

End Sub

We must enter the mixture of red, green, and blue numbers. To use this, you should be perfect in entering the numbers. We have entered the mixture as RGB (250, 200, 150).

Sub Color()

Range(“A1”).Interior.Color = RGB(250, 200, 150)

End Sub

The change of this color is like this.

The problem here is you do not know what number holds what color.

The below code is to change the font color.

Sub Color_Font()

Range(“A1”).Font.Color = RGB(100, 400, 100)

End Sub

Run this code using the F5 key or manually to get the result.

#2 – Using Color Index Property

Color Index is slightly different from the color property. For example, in the “COLOR” property, we use 8 constant colors given by VBA. Also, using the RGB function, we create our color.

The “COLOR INDEX” property is limited in VBA. It can hold values from 1 to 56. Each number from 1 to 56 holds different colors. Below are the list numbers and their respective colors.

Below is the macro code to change the background color of cell A1 to pink.

Sub ColorIndex_Cell()

Range(“A1”).Interior.ColorIndex = 26

End Sub

Run this code manually or use the F5 key to see the result.

Below is the macro code to change the font color of cell A1 to yellow.

Sub ColorIndex_Font()

Range(“A1”).Font.ColorIndex = 27

End Sub

Run this code using the F5 key, or you can run it manually and see the result.

One of the important things to remember here is we can enter numbers from 1 to 56. If anything more than 56, we will get the “Subscript Out Of Range” error.

Did You Observe?

As we said in the “Color Index,” the property can insert only 56 colors, but the fact is that it can only insert 46 unique colors, and there are 10 duplicate colors. Below is the list of duplicate color codes.

This article is a guide to the VBA Color Index. Here, we learn the top ways to use the color and color index property in Excel VBA, along with some simple to advanced examples. Below are some useful Excel articles related to VBA: –