Chapter 12. Automating Procedures with Workbook Events

In Lesson 11, you learned about worksheet-level events and how they are triggered by actions relating to individual worksheets. Workbooks themselves can also recognize and respond to a number of events that take place at the workbook level. This lesson describes how you can further customize your workbooks with VBA procedures for the most commonly used workbook events.

WORKBOOK EVENTS — AN OVERVIEW

Workbook events occur within a particular workbook. Many workbook events occur because something happened to an object in the workbook, such as a worksheet — any worksheet — that was activated, or a cell — any cell — that was changed. Other workbook events occur because the workbook was imposed upon to do something, such as to open or close, or to be saved or printed.

Note

Unless the VBA code itself purposely refers to other workbooks, event procedures at the workbook level affect only the workbook within which the code resides.

Where Does the Workbook Event Code Go?

You saw in Lesson 11 that each individual worksheet has its own module. Workbooks are similar to worksheets in this respect, because a workbook is also an Excel object, and has its own module already present and accounted for when the workbook is created.

Note

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 ...

Get Excel® VBA: 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.