Event Procedures

You have already seen a few examples of event procedures; such as the Click() event procedure of a Command Button control, and the SelectionChange() event procedure of a worksheet. VBA predefines these procedures in the sense that you cannot change the name of the procedure, nor the object within Excel to which the procedure belongs, nor the conditions under which the procedure is triggered. For the most part, all you can do with these procedures is add the code to be executed when the event is triggered. Typically, several events are associated with each Excel object; whether it is a worksheet, workbook, chart, or ActiveX control. Figure 3.2 shows the object module for a worksheet and displays all of the events associated with a worksheet in Excel.

Dropdown list of events

Worksheet events in Excel.

Dropdown list of events

Worksheet events in Excel.

Event Procedures

Event procedures are defined with the Sub keyword followed by the name of the procedure.

Private Sub Worksheet_Activate()

'Event procedure code is listed here. End Sub

The name of the procedure listed above is Worksheet_Activate(), although it will be more commonly referred to as the Activate() event. No parameters are passed to this procedure because the parentheses are empty. This procedure is triggered when the worksheet to which it is associated is activated; that is, when you switch between two different windows or worksheets, the Activate() event of the currently selected worksheet is triggered. The procedure ends with the line End Sub, unless the statement Exit Sub is used within the procedure code.

Biorhythm Awareness

Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook


Post a comment