VBA Color Index

Excel VBA Color Index

Like in a worksheet we change the color of a cell or a given cell range from the home tab in VBA. We have a vba function called as Color Index which is used to change the colors of the cells or the cell range provided, this functions has unique identification for different type of colors.

In VBA, we have two ways of applying the color, the first one is by using the “color” property, and the second one is using the “ColorIndex” property.

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

VBA Color Index

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

How to use Color & Color Index Property?

You can download this VBA Color Index Template here – VBA Color Index Template

#1 – Using Color Property

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

VBA Color Index Example 1

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

Code:

Sub Color()

 Range ("A1")

End Sub
VBA Color Index Example 1-1

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

Code:

Sub Color()

  Range("A1").Interior

End Sub
VBA Color Index Example 1-2

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

Code:

Sub Color()

 Range("A1").Interior.Color =

End Sub
Visual Basic interior color 1-3

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

vbBlack, vbRed, vbGreen, vbBlue, vbYellow, vbMagenta, vbCyan, vbWhite

Select the color type as per your wish.

Code:

Sub Color()

 Range("A1").Interior.Color = vbBlue

End Sub
Visual Basic interior color 1-4

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

VBA Color Index Example 1-5

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

Apart from these 8 constant colors, we can also fill with different colors as well by using the RGB function in VBA.

Code:

Sub Color()

 Range("A1").Interior.Color = RGB(

End Sub
VBA Color Index Example 1-6

We need to enter the mixture of Red, Green, and Blue in numbers. To use this, you should be perfect in entering the numbers. I have entered the mixture as RGB (250, 200, 150).

Code:

Sub Color()

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

End Sub
Visual Basic RGB 1-7

The change of this color is like this.

VBA Color index Example 1-8

The problem here is you don’t know exactly what number holds what color.

The below code is to change the font color.

Code:

Sub Color_Font()

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

End Sub
VBA Color index Example 1-9

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

VBA Color index Example 1-10

#2 – Using Color Index Property

Color Index is slightly different from the color property. In the COLOR property, we use 8 constant colors given by VBA and also using the RGB function, we create our own color.

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

Color Index

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

Code:

Sub ColorIndex_Cell()

Range("A1").Interior.ColorIndex = 26

End Sub
VBA Color Index Example 2

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

VBA Color Index Example 2-1

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

Code:

Sub ColorIndex_Font()

Range("A1").Font.ColorIndex = 27

End Sub
Visual Basic font color 2-4

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

VBA Color Index Example 2-2

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.

VBA Color Index Example 2-3

Did You Observe?

As I told 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.

Duplicate color codes

Recommended Articles

This has been a guide to VBA Color Index. Here we learn the top ways to use Color and Color Index property in Excel VBA along with some simple to advanced examplesBelow are some useful excel articles related to VBA –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>