VBA Course: Colors

We are going to start by assigning a color to the text in A1.

After adding Font., we get:

color en colors

We have 2 possibilities to define the color: ColorIndex and its 56 colors or Color that will allow us to use any color.

ColorIndex

Here are the 56 colors available with ColorIndex:

colorindex en colors

To apply one of these 56 colors to our text, we will write:

Sub colors()

    'Text color in A1: blue (color 23)
    Range("A1").Font.ColorIndex = 23

End Sub

This gives us:

test2 en colors

For versions of Excel prior to 2007: using ColorIndex is preferable to Color.

Color

Here is a similar example with Color:

Sub colors()

    'Text color in A1: RGB(0, 102, 204)
    Range("A1").Font.Color = RGB(0, 102, 204)

End Sub

The color here is RGB(0, 102, 204).

RGB stands for Red Green Blue (the values range from 0 to 255 for each color).

Some examples of colors to better understand:

Luckily for us, there are different solutions that allow us to easily find the RGB values of the color that interests us.

You will find a list of RGB values on the following page: RGB Colors.

To give a purple color to our text, we can therefore search for the RGB values of this color on the color list and enter:

Sub colors()

    'Text color in A1: RGB(192, 32, 224)
    Range("A1").Font.Color = RGB(192, 32, 224)

End Sub

This gives us:

test en colors

For versions of Excel prior to 2007: the number of colors is limited (the closest available color to the RGB value will be used).

Create a colored border

We are going to create a macro that will add a border to the active cell with ActiveCell.

The border will be red and thick:

Sub colors()

    'Border thickness
    ActiveCell.Borders.Weight = 4

    'Border color: red
    ActiveCell.Borders.Color = RGB(255, 0, 0)

End Sub

Preview:

border en colors

Color the background of the selected cells

Sub colors()

    'Color the background of the selected cells
    Selection.Interior.Color = RGB(215, 238, 247)

End Sub

Preview:

background en colors

Color a sheet tab

Sub colors()

    'Color the tab of the sheet "Sheet1"
    Sheets("Sheet1").Tab.Color = RGB(255, 0, 0)

End Sub

Preview:

tab en colors