Databases Reference
In-Depth Information
5.5
HIGHLIGHTING AND COLORING CELL
FONTS AND BACKGROUNDS
One of the most useful ways to call attention to information of interest on a Worksheet is to apply
color to either the text that contains the information or the background of the cell that holds the
information. This is not a difficult task to accomplish utilizing VBA. Two sample applications have
been constructed to show the reader how to accomplish these tasks. The first shows how to change
the color of the font in a particular cell or group of cells.
Figure 5.11 shows the GUI for the Color Cell Font sample application, which can be run from
the menu A DA->Chapter 5 ->Color Cell F ont. The user must choose a selection on the Worksheet
utilizing the RefEdit control. A color must then be selected from the drop-down list. Notice that
this application makes use of the standard VB colors utilizing the VB color constants discussed
earlier in this chapter. Once that has been done, the user also has the option of setting the font to
bold by means of a checkbox. Finally, the user may change the color of the font in the range
selected by the RefEdit Control by simply pressing the “Change Font Color” button. The mecha-
nisms that allow this sample to work are now discussed.
Upon startup the CreateColorArray subroutine previously developed in this chapter is
utilized to populate the combo box with the VB Color Constants.
Private Sub UserForm_Activate()
Dim ii As Integer
Call CreateColorArray(Colors())
For ii = 1 To UBound(Colors(), 1)
frm5ChangeFont.ColorComboBox.AddItem Colors(ii, 1)
Next ii
End Sub
When the “Change Font Color” button is pressed, the program checks to determine if a valid
range is present in the RefEdit control and if a valid color is present in the combo box. If both
selections are valid, then the color of the font in the specified region is changed. The following
snippet of code accomplishes this.
FIGURE 5.11 The Color Cell Font sample application.
Search WWH ::




Custom Search