Excel 2010 Bible

(National Geographic (Little) Kids) #1

Part VI: Programming Excel with VBA


882


Using the SelectionChange event

The following procedure demonstrates a SelectionChange event. It executes whenever the user
makes a new selection on the worksheet:

Private Sub Worksheet_SelectionChange(ByVal Target _
As Excel.Range)
Cells.Interior.ColorIndex = xlNone
With ActiveCell
.EntireRow.Interior.ColorIndex = 35
.EntireColumn.Interior.ColorIndex = 35
End With
End Sub

This procedure shades the row and column of an active cell, making it easy to identify. The first
statement removes the background color of all cells. Next, the entire row and column of the active
cell is shaded light yellow. Figure 43.2 shows the shading.

On the CD
A workbook with this example is available On the CD-ROM. The file is named selection change
event.xlsm.


FIGURE 43.2

Moving the cell cursor causes the active cell’s row and column to become shaded.

Free download pdf