Font

 

The Font property of the Range object in Excel VBA gives access to a lot of other properties. That is because the Font property returns an object itself; the Font object. The Font object has many properties like the Color property and the Bold property.

Color property

To change the color of an Excel range, use the Font property of the Range object, and then the Color property of the Font object.

1. Add the following code line:

Range("A1").Font.Color = -16776961

Explanation: Where do we get this strange number from? Well, we started the Macro Recorder and changed the color of a cell to red. You can to this for every color!

2. The following code line gives the exact same result.

Range("A1").Font.Color = vbRed

Explanation: vbRed is a sort of built-in constant in Excel VBA. Place your cursor on vbRed in the Visual Basic Editor and click F1 to see which other constants you can use.

3. The following code line gives the exact same result.

Range("A1").Font.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.

Bold property

The following code line bolds a range:

Range("A1").Font.Bold = True

To unbold a range, you can use the False keyword. The Font object has many more properties. If you want to program these sort of things, just use the Macro Recorder to see how to do it! Usually code created by the Macro Recorder is too long. For example, the Macro Recorder creates the following code when we bold Range("A1").

Font and Bold Property in Excel VBA

We have just seen that these two code lines can be written as one code line.