Cell Formatting
To apply formatting to a cell, you use the Interior object. For example,
ActiveCell.Interior.Pattern = xlSolid
sets the active cells background pattern to a solid. Its other main properties
are shown below:
|
Property Name
|
| Color |
| ColorIndex |
| Pattern |
| PatternColor |
| PatternColorIndex |
As explained in the last section, using ColorIndex you can specify a color
from the standard palette, or using the Color property you can specify an exact
colour.
The possible values for the Pattern property are shown below:
|
xlPatternAutomatic
xlPatternChecker
xlPatternCrissCross
xlPatternDown
xlPatternGray16
xlPatternGray25
xlPatternGray50
xlPatternGray75
xlPatternGray8
xlPatternGrid
|
xlPatternHorizontal
xlPatternLightDown
xlPatternLightHorizontal
xlPatternLightUp
xlPatternLightVertical
xlPatternNone
xlPatternSemiGray75
xlPatternSolid
xlPatternUp
xlPatternVertical
|
For example, the following code sets the background colour to blue, the pattern
to red horizontal lines:
With ActiveCell.Interior
.Color = RGB(0,0,255)
.Pattern = xlPatternHorizontal
.PatternColor = RGB(255,0,0)
End With
You can also set a cells border using the Borders object and BordersAround
object. With these, you can set the following:
|
Property Name
|
| Color |
| ColorIndex |
| Item |
| LineStyle |
| Value |
| Weight |
The Item property allows you to set a specific border. You can use the BordersAround
object to set the 'perimeter' border for a selection of cells
The following code sets the active cell's border to red, and makes the top
border thicker:
ActiveCell.Borders.Color = RGB(255,0,0)
ActiveCell.Borders.Item(xlEdgeTop).Weight = xlThick
For more detailed help, type Range("A2").Borders (or another valid
range... not ActiveCell as VB doesn't know what that is until the code is being
run) and press F1