Entering event handler code

Every event handler procedure has a predetermined name. Following are some examples of event handler procedure names:

♦ Worksheet_SelectionChange

♦ Workbook_Open

♦ Chart_Activate

♦ Class_Initialize

You can declare the procedure by typing it manually, but a much better approach is to let the VBE do it for you.

Figure 19-2 shows the code module for the ThisWorkbook object. To insert a procedure declaration, select Workbook from the objects list on the left. Then select the event from the procedures list on the right. When you do so, you get a procedure "shell" that contains the procedure declaration line and an End Sub statement.

\w

Bookl -ThisWorkbook (Code)

m

| jworkltook

~3

| Open

I

Private Sub tJorkbook Open (

)

Deactivate NewSheet

-

End Sub

PivotTableCloseConnection Hj

PivotTableOpenConnection

SheetActivate

Sheet BeforeDoubleClick

Sheet BeforeRightClick

Sheet Calculate

Sheet Change

Sheet Deactivate

SheetFollowHyperlink

-

-

Figure 19-2: The best way to create an event procedure is to let the VBE do it for you.

Figure 19-2: The best way to create an event procedure is to let the VBE do it for you.

For example, if you select Workbook from the objects list and Open from the procedures list, the VBE inserts the following (empty) procedure:

Private Sub Workbook_Open()

End Sub

Your code, of course, goes between these two statements.

0 0

Post a comment