CHAPTER 43Working with Excel Events

In the preceding chapters, we presented a few examples of VBA event-handler procedures for ActiveX controls. These procedures are the keys to making your Excel applications interactive. This chapter introduces the concept of events for Excel objects and includes many examples that you can adapt to meet your own needs.

Understanding Events

Excel monitors a variety of events and can execute your VBA code when any of these events occur. This chapter covers the following types of events:

  • Workbook events  These occur for a particular workbook. Examples include Open (the workbook is opened or created), BeforeSave (the workbook is about to be saved), and NewSheet (a new sheet is added). You must store VBA code for workbook events in the ThisWorkbook code module.
  • Worksheet events  These occur for a particular worksheet. Examples include Change (a cell on the sheet is changed), SelectionChange (the selection on the worksheet is changed), and Calculate (the worksheet is recalculated). VBA code for worksheet events must be stored in the code module for the worksheet (for example, the module named Sheet1).
  • Special Application events  The final category consists of two useful application-level events: OnTime and OnKey. These are different from other events because the code isn't in a class module. Rather, you set the ...

Get Microsoft Excel 365 Bible 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.