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 VBA 365 code that formats the color of an MS Excel spreadsheet. Using Excel VBA 365code, 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 365, 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

cells(i,j).Font.Color=RGB(x,y,x)

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

For example

cells(1,1).Font.Color=RGB(255,255,0)

will change the font color to yellow

The syntax to set the cell’s background color is

cells(i,j).Interior.Color=RGB(x,y,x)

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
(0,0,0)
(255,0,0)
(255,255,0)
(255,165,0)
(0,0,255)
(0,128,0)
(128,0,128)
(0,255,255)
(128,0,0)
(128,128,128)
(255,0,255)
(220,20,60)





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

Contact: Facebook Page