Excecl VBA Classic Excel VBA 2010 Excel VBA 365 Excel VBA Examples About Us

Lesson 21 Formatting Colors

In this Lesson, we will explore how to write Excel 2010 VBA code that formats the color of an MS Excel spreadsheet. Using Excel 2010 VBA code, we can change the font color as well as the background color of each cell effortlessly.

Alright, let’s create a program that can format random font and background colors using a randomizing process. Colors can be assigned using a number of methods in Excel VBA 2010, but it is easier to use the RGB function. The RGB function has three numbers corresponding to the red, green and blue components. The range of values of the three numbers is from 0 to 255. A mixture of the three primary colors will produce different colors.

The syntax to set the font color is


where x ,y , z are any numbers between 1 and 255

For example


will change the font color to yellow

The syntax to set the cell’s background color is


Where x ,y , z can be any number between 1 and 255

Some RGB Color Codes are shown in the following table

Table 21.1 Color Code
Color RGB Code

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved   [Privacy Policy]

Contact: Facebook Page