In general, event is nothing but automatically triggering a program based on some action or other program. In Microsoft Excel also through events we can trigger a program to run whenever user did something in Excel or because of some other program in Excel.
In Microsoft Office Excel we have four types of Events. Those are Application Level, Workbook Level, Worksheet Level or Chart sheet Level. Today we discussed about various Workbook level events of excel.
Workbook level events occurs whenever some action happened at workbook level. For example if we open workbook, Workbook_Open event occurs. All workbook level events goes into the ThisWorkbook module.
Let’s discuss each Workbook Level event.
Workbook_Open(): This event is the default event of workbook. Workbook_Open() event occurs whenever we opens the excel workbook. This does not requires any user interface, it automatically occurs whenever workbook opens.
Open Microsoft Office 2013 in designer mode and write below code.
Private Sub Workbook_Open()
MsgBox (Application.UserName)
End Sub
The above code executes whenever you open the xlsm file and displays the application username.
Workbook_Activate(): Whenever any Microsoft Excel workbook contains this event, that workbook automatically becomes active.
Workbook_DeActivate(): This event occurs whenever active workbook switched from the workbook containing this event to another workbook.
Workbook_BeforeSave: This event occurs before save event happens. It returns two boolean parameters, those are SaveAsUI and Cancel. If the save operation success SaveAsUI returns true and if not Cancel returns as True.
Workbook_AfterSave: This event occurs after the workbook saved and this event returns True if the workbook saved successfully or it will return false.
Workbook_BeforePrint: This event occurs whenever any print command executes. For example if we enter Ctrl+P, Workbook_BeforePrint event executes first.
Workbook_BeforeClose: This event occurs whenever workbook closes. It takes one parameter Cancel as Boolen. Cancel has set to True to prevent close operation.
Workbook_NewSheet: This event occurs when new sheet added to the active workbook.
Workbook_SheetBeforeDelete: This event occurs before deleting any sheet from active workbook.
Workbook_NewChart: This event occurs whenever new chart is added to the active workbook.
Workbook_WindowResize: This event occurs whenever we resizes the active excel workbook.
Workbook_WindowActivate: This event occurs whenever we activates any workbook window.
Workbook_WindowDeactivate: This event occurs whenever we deactivates any active workbook window.
Workbook_AddInInstall: This event occurs whenever we install any AddIn in the active workbook.
Workbook_AddInUnInstall: This event occurs whenever we uninstall the AddIn from the active workbook.
Workbook_Sync: This event occurs whenever we synchronizes the local active workbook with the server copy.
Workbook_PivotTableOpenConnection: This event occurs whenever connection opens between the active workbook pivot table and the data source.
Workbook_PivotTableCloseConnection: This event occurs whenever connection closes between the active workbook pivot table and the data source.
Workbook_RowsetComplete: It occurs whenever we drills through the recordset or calls upon the rowset action on an OLAP pivot table.
Workbook_BeforeXmlExport: This event occurs whenever users exports the XML data.
Workbook_AfterXmlExport: This event occurs whenever after users exports the XML data.
Workbook_BeforeXmlImport: This event occurs whenever users imports the XML data.
Workbook_AfterXmlImport: This event occurs whenever after users imports the XML data.
Workbook_ModelChange: This event occurs whenever user changes the data model in the active workbook.