Excel

Workbook Events

Workbook events fires when a user performs such tasks as opening, closing, activating, deactivating, printing, and saving a workbook. Workbook events are not created in a standard VBA module. To write code that responds to a particular workbook you you must access the VBE (Visual Basic Editor) and then open the code window of the ThisWorkbook object.

To open VBE press Alt + F11 (For more information, see How to open Visual Basic Editor), for writing an event-handler for the workbook, you need to click ThisWorkbook in the Project window and then choose Workbook from the Object drop-down, the next drop-down displays a list of all workbook events, as shown in the following figure:

Workbook Object Events

  1. Double click ThisWorkbook object to open its code window
  2. Select Workbook from the Object drop-down list
  3. The Procedure drop-down list shows the list of event procedures

List of Workbook Events

  1. Activate
    The Workbook_Activate event procedure is executed when the workbook is activated.

  2. Deactivate
    The Workbook_Deactivate event procedure is executed when the workbook loses focus.

  3. AddinInstall
    The Workbook_AddinInstall event procedure is executed when the add-in is installed.

  4. AddinUninstall
    The Workbook_AddinUninstall event procedure is executed when the add-in is just uninstalled.

  5. AfterRemoteChange
    The Workbook_AfterRemoteChange event procedure is executed when changes are merged into the workbook by a remote user.

  6. BeforeRemoteChange
    The Workbook_BeforeRemoteChange event procedure is executed just before the changes are merged into the workbook by a remote user.

  7. AfterSave
    The Workbook_AfterSave event procedure is executed when the workbook is saved.

  8. BeforeSave
    The Workbook_BeforeSave event procedure is executed just before the workbook is saved. This event can be helpful to prevent Save As command.

  9. AfterXmlExport
    The Workbook_AfterXmlExport event procedure is executed when Excel saves or exports XML data from the workbook.

  10. AfterXmlImport
    The Workbook_AfterXmlImport event procedure is executed when an existing XML data connection is refreshed or new XML data is imported into the workbook.

  11. BeforeXmlExport
    The Workbook_BeforeXmlExport event procedure is executed just before the Excel saves or exports XML data from the workbook.

  12. BeforeXmlImport
    The Workbook_BeforeXmlImport event procedure is executed before XML data connection is refreshed or before new XML data is imported into the workbook

  13. BeforeClose
    The Workbook_BeforeClose event procedure is executed just before the workbook closes.

  14. Open
    The Workbook_Open event is executed when the workbook opens.

  15. BeforePrint
    The Workbook_BeforePrint event procedure is executed before a user attempts to print any portion of the workbook.

  16. ModelChange
    The Workbook_ModelChange event procedure is executed when a user changes the Data Model (for example, columns added or deleted).

  17. NewChart
    The Workbook_NewChart event procedure is executed when a new chart is added to the workbook.

  18. NewSheet
    The Workbook_NewSheet event procedure is executed when a new worksheet is added to the workbook.

  19. SheetActivate
    The Workbook_SheetActivate event procedure is executed when a worksheet is activated in the workbook.

  20. SheetDeactivate
    The Workbook_SheetDeactivate event procedure is executed when a worksheet loses focus, such as when a different sheet in the workbook is activated.

  21. SheetBeforeDelete
    The Workbook_SheetBeforeDelete event procedure is executed before any worksheet in the workbook is deleted.

  22. SheetBeforeDoubleClick
    The Workbook_SheetBeforeDoubleClick event procedure is executed when a cell on any worksheet is about to be double-clicked.

  23. SheetBeforeRightClick
    The Workbook_SheetBeforeRightClick event procedure is executed when a cell on any worksheet is about to be right-clicked.

  24. SheetCalculate
    The Workbook_SheetCalculate event procedure is executed when a user recalculates any worksheet.

  25. SheetChange
    The Workbook_SheetChange event procedure is executed when any cell’s contents are changed on any worksheet in the workbook.

  26. SheetSelectionChange
    The Workbook_SheetSelectionChange event procedure is executed when a different cell is selected on any worksheet in the workbook.

  27. SheetFollowHyperlink
    The Workbook_SheetFollowHyperlink event procedure is executed when a user click any hyperlink on any worksheet in the workbook.

  28. SheetLensGalleryRenderComplete
    The Workbook_SheetLensGalleryRenderComplete event procedure is executed when a user selects the Quick Analysis tool.

  29. SheetTableUpdate
    The Workbook_SheetTableUpdate event procedure is executed when the user changes a table object.

  30. RowsetComplete
    The Workbook_RowsetComplete event procedure is executed when a user either drills through the recordset or invokes the rowset action on an OLAP PivotTable.

  31. PivotTableCloseConnection
    The Workbook_PivotTableCloseConnection event procedure is executed when a pivot table closed the connection to its data source.

  32. PivotTableOpenConnection
    The Workbook_PivotTableOpenConnection event procedure is executed when a PivotTable report opens the connection to its data source.

  33. SheetPivotTableAfterValueChange
    The Workbook_SheetPivotTableAfterValueChange event procedure is executed when a cell or range of cells that contain formulas inside a PivotTable are edited or recalculated.

  34. SheetPivotTableBeforeAllocateChanges
    The Workbook_SheetPivotTableBeforeAllocateChanges event procedure is executed before changes are applied to a PivotTable.

  35. SheetPivotTableBeforeCommitChanges
    The Workbook_SheetPivotTableBeforeCommitChanges event procedure is executed before changes are committed against the OLAP data source for a PivotTable.

  36. SheetPivotTableBeforeDiscardChanges
    The Workbook_SheetPivotTableBeforeDiscardChanges event procedure is executed before changes to a PivotTable are discarded.

  37. SheetPivotTableChangeSync
    The Workbook_SheetPivotTableChangeSync event procedure executed after a user changes a pivot table.

  38. SheetPivotTableUpdate
    The Workbook_SheetPivotTableUpdate event procedure executed when any worksheet get updated in the workbook that hold pivot tables.

  39. Sync
    The Workbook_Sync (deprecated, you should not use it) procedure is executed when a user synchronizes the local copy of a sheet in a workbook that is part of a Document Workspace with the copy on the server.

  40. WindowActivate
    The Workbook_WindowActivate event procedure is executed when user shifts the focus to any window showing the workbook.

  41. WindowDeactivate
    The Workbook_WindowDeactivate event procedure is executed when the user shifts the focus away from any window showing the workbook.

  42. WindowResize
    The Workbook_WindowResize event procedure is executed when a user opens, resizes, maximizes, or minimizes any window showing the workbook.