Background Colors


Changing background colors in Excel VBA is easy. Use the Interior property to return an Interior object. Then use the ColorIndex property of the Interior object to set the background color of a cell.

Place three command buttons on your worksheet and add the following code lines:

1. The code line below sets the background color of cell A1 to light blue.

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


Background Color in Excel VBA

2. The following code line sets the background color of cell A1 to 'No Fill'.

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


No Fill

3. If you want to know the ColorIndex number of a color, simply ask Excel VBA.

MsgBox Selection.Interior.ColorIndex

Select cell A1 and click the command button on the sheet:

Get ColorIndex Number

4. The ColorIndex property gives access to 56 "preset" colors. If you can't find the specific color you are looking for, use the Color property and the RGB function.

Range("A1").Interior.Color = RGB(255, 0, 0)

Explanation: RGB stands for Red, Green and Blue. These are the three primary colors. Each component can take on a value from 0 to 255. With this function you can make every color. RGB(255,0,0) gives the pure Red color (exact same result as above).

Do you like this free website? Please follow us on Google+