Microsoft Office Tutorials and References
In Depth Information
Lesson 12: Automating Procedures with Workbook Events
Workbook-level event code always goes into the workbook module. You never
need to create a workbook module or a worksheet module; Excel creates those
modules automatically with every new workbook. If a workbook-level event
procedure is not in the workbook module (same as if a worksheet-level event
procedure is not in a worksheet module), VBA will not be able to execute the
To arrive at the Code window for your
workbook’s module, with whatever
version of Excel you are using, you can
press Alt+F11 to get into the Visual Basic
Editor. If you are using a version of Excel
prior to 2007, such as version 2003, you
can also access the workbook module
quickly by right-clicking the Excel
workbook icon near the top-left corner of the
workbook window, and selecting View
Code. This option is shown in Figure 12-1.
In Excel versions prior to 2007,
to access the workbook module quickly,
right-click the workbook icon to the left of
the File menu item, and select View Code.
In the VBE, if you do not see the Project Explorer window, go ahead and make it visible by
pressing Ctrl+R. In the Project Explorer, find your workbook name; it will be in bold font, with the text
VBAProject (YourWorkbookName.xlsm) . Directly below that will be a yellow folder named
Microsoft Excel Objects. When that folder is expanded, the last item at the bottom of the list is the
workbook object, identified by its default name of ThisWorkbook .
As shown in Figure 12-2, to get into the Code window of the workbook module, either
doubleclick the ThisWorkbook object, or right-click it and select View Code. As soon as you do that, your
mouse cursor will be blinking in the workbook module’s Code window, ready for you to start
entering your workbook-level event procedure code.
To access the workbook’s module, either...
...double-click the ThisWorkbook object
...right-click the This Workbook object,
and select ViewCode.