Microsoft Office Tutorials and References
In Depth Information
Figure 11-1. Events for the Worksheet object
By now you are probably wondering how to write an event procedure. The short answer is that for
each event, Excel provides us with an event code shell where we can place the event code for that
To illustrate, consider the SelectionChange event of the Worksheet object. Figure 11-1 s hows the
code window for a worksheet (Sheet1). Note that the Worksheet object is selected in the objects
list box. This causes the procedures list box to be filled with the names of the worksheet events.
We can simply choose the event for which we want to write event code.
Figure 11-1. Events for the Worksheet object
For instance, if we choose the SelectionChange event, Excel will automatically produce the
following code shell:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
End Sub
Excel will even place the cursor between the two code lines so we can begin entering event code.
As the name implies, this event fires when the current selection is changed in the worksheet. Note
that Excel will fill in the Target parameter with the Range object that represents the new
selection. Thus, our event code has access to the new selection, but not to the previous selection.
Many events have parameters associated with them. This provides a way for Excel to pass us
information related to the event.
The same approach will work for the workbook and chart events, but Application events require a
different approach, which we will discuss later in the chapter.
The Excel events are listed in Table 11-1 t hrough Table 11-5 .
11.4 Worksheet Events
The worksheet-related events are shown in Table 11-1 . These events are also referred to as
document events.
Table 1-1. Worksheet Events (DocEvents) 1
Event name
Occurs when a worksheet is activated.
Occurs when a worksheet is double-clicked, before the default
double-click action.
Search JabSto ::

Custom Search