- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
VBA: How to Change Excel Font and Cell Background Color
Changing Font Color
Changing the color a font in excel is fairly easy and straight forward test.
- Select the cell or range
- Right Click
- Select Format Cells
- Click on the font tab
- Find the color field
- Click the drop down button
Notice you have the choice of picking from a variety of preset colors. You also have the option of selecting custom color. This will take you to a color wheel where you can pick the exact color you want.
Changing Cell Background Color
The background color is also change within the cell format toolbox.
- Click the fill tab.
- Select desired color.
Notice again the option for more colors, once again this will take you to a color wheel where you can select any color you want. See below for illustration.
Notice how you color is defined by Red, Green, and Blue coordinates. This is how you will define your color within VBA.
Change Font Color VBA
'Change Color of Range "A1" to the green defined above
Range("A1").Font.Color = RGB(59, 179, 73)
Change Background Color VBA
'Change color of Background of Range "A1" to green color define above
Range("A1").Interior.Color = RGB(59, 179, 73)
A Test Macro
Sub test() Range("A1") = "Fun" Range("A1").Font.Size = 72 Range("A1").Interior.Color = RGB(59, 179, 73) Range("A2") = "With" Range("A2").Font.Size = 72 Range("A2").Font.Color = RGB(99, 179, 73) Range("A3") = "Colors" Range("A3").Font.Size = 72 Range("A3").Font.Color = RGB(99, 179, 73) Range("A3").Interior.Color = RGB(0, 0, 0) End Sub
Ready to Proceed?
You will notice the font is too large for the column. We will soon learn to set the width of columns and use autofit, but first lets learn how to select and reference columns and rows within Excel and Visual Basic.