Worksheet Events Procedures

Worksheet events occur for a particular worksheet when something happens to that worksheet. For example, when Sheet1 gets active the Worksheet.Activate event occurs for Sheet1 and when you leave the Sheet1 and/or select another sheet (such as Sheet2) the Sheet1 fires the Worksheet.Deactivate event. Event code that is associated with any particular worksheet has no direct effect on events that take place on other worksheets in that or any other workbook.

List of available worksheet event in Excel:

Writing the Worksheet Event Code

Worksheet event procedures must be entered into the code module for the worksheet. Each worksheet comes with its own built-in module, these code modules have default names like Sheet1, Sheet2, Sheet3 and so on. To open a code module, right click on its worksheet tab and select View Code, as shown in following figure:

Worksheet View Code Context Menu

Immediately after you select View Code, you are taken directly into the VBE (Visual Basic Editor) where you can start entering the event procedure code.

Worksheet code window

Immediately above the Code window you'll see two dropdown lists. The first dropdown list says (General) is called "Object List Box" and the second one says (Declarations) is called "Procedure List Box". Click the Object (first dropdown) list and select the Worksheet. It will write the Worksheet_SelectionChange event procedure on the coding area. Now all the Event procedures are available on the second dropdown list as shown in following figure:

The Object and Precedure List Box

In next tutorials we'll read about the Worksheet events by writing some examples for each event procedure.