Font in Excel VBA
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:
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 do this for every color!
2. The following code line gives the exact same result.
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.
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:
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").
We have just seen that these two code lines can be written as one code line.
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.