The Interior property

Here's another example of a property that returns an object. A Range object's Interior property returns an Interior object (strange name, but that's what it's called). This type of object referencing works the same way as the Font property (which I describe in the preceding section).

For example, the following statement changes the Color property of the Interior object contained in the Range object:

Range("A1").Interior.Color = 8421504

In other words, this statement changes the cell's background to middle gray.

The Color property values range from 0 to 16777215. Another way to specify colors is to use VBA's RGB function. This function takes three arguments, which correspond to the color's red, green, and blue components. Each of these arguments can range from 0 to 255. Following are a few examples that use the RGB function to change a cell's background color:

Range(

" A1"

).Interior.

.Color =

= RGB(0, 0,

0) '

black

Range(

" A1"

).Interior.

.Color =

= RGB(255,

0, 0)

' pure red

Range(

" A1"

).Interior.

.Color =

= RGB(0, 0,

255)

' pure blue

Range(

" A1"

).Interior.

.Color =

= RGB(128,

128,

128) ' middle

gray

If you need to use standard colors, you may prefer to use one of the built-in color constants: vbBlack, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan, or vbWhite. For example, the following statement makes cell A1 yellow:

Range("A1").Interior.Color = vbYellow ri

Previous versions of Excel supported only 56 different colors for cells. Excel 2007 supports more than 16 million colors, and also has a new feature known as Themes. Applying a new theme to a workbook can change the colors. Bottom line? Working with colors has become a lot more confusing. But some of us think it's fun.

0 0

Post a comment