Cell Background Colors in Excel VBA

 

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

Result:

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

Result:

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

Result:

ColorIndex Number

4. The ColorIndex property gives access to a color palette of 56 colors.

Color Palette

Note: download the Excel file to see how we created this color palette.

5. 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 (ColorIndex = 3 produces the exact same result).

If you're new here, welcome to Excel Easy! Join over 1 million monthly Excel learners. You can find popular courses here: Data Analysis in Excel and Excel VBA.