VBA ColorIndex

Colors

Colors could be very important and useful in excel application. There are two ways in which you can get or set the colors in VBA Excel. They are: ColorIndex, which has 56 colors, and Color, which has millions of colors and makes it possible to use any color at all.

ColorIndex

The ColorIndex is also a property. It is used to assign a certain color to some elements, such as a cell’s background or its text, from a palette of 56 pre-defined colors, via VBA programming.

In order to use this ColorIndex property, you will need to know each color’s corresponding value in the color palette. You can find out these values by running the following macros:

VBA ColorIndex
Fig 2.7 Getting the color palette

We can now use this color palette to set the color of any cell, group of cells or text.

As an example, we will set the color of cell “B3” using a value from the palette. Copy the following macros to your VB Editor:

Fig 2.9 Macro for ColorIndex
Fig 2.10 Result

Excel RGB Color

The other method of using the color property in Excel is through the RGB color property “RGB” stands for Red Green Blue, which are known as three primary colors, which can be combined to produce other colors. For example, purple is a mix of blue and red.  In RGB Property, you need to give a value (from 0 to 255) for each color in order to mix a new color. For example, Red = 255 means the brightness of color red is 100%. If notice, we’ve been using this method to access the color property of Excel. The below macro changes the color of the selected cells using this RGB method;

VBA ColorIndex
2.11 RGB Macro
VBA ColorIndex
Fig 2.12 Result

However, these are just few of what you can do with all the discussed methods and properties.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Send this to a friend