Workbook_Activate Event Procedure
The Workbook_Activate
event occurs when the workbook is activated. For example, when the workbook is opened, or when Excel has more than one workbook opened and the workbook is activated (switch between that workbook).
Note: This event will not occur when you activate the workbook by switching from another application.
Example: Workbook_Activate() Event Procedure
- Open a new workbook and save it as
BrainBell.xlsm
. - Press
Alt + F11
to open the Visual Basic Editor window - In the Project Explorer window, double-click
ThisWorkbook
- In the code window, type the
Workbook_Activate
event procedure:
Private Sub Workbook_Activate() MsgBox ThisWorkbook.Name End Sub
To check if the event procedure working or not, switch to the Excel window and open a new workbook. Now switch back to workbook containing the event procedure (in our example BrainBell.xlsm
). Excel should display the name of the workbook.
Workbook_Deactivate Event Procedure
The Workbook_Deactivate
event occurs when the workbook loses focus. For example, when you open another workbook or close the workbook containing deactivate event procedure. This event does not occur when you switch to a different application.
Example: Workbook_Deactivate() Event Procedure
- Open the
BrainBell.xlsm
workbook - Press
Alt + F11
to open the Visual Basic Editor window - In the Project Explorer window, double-click
ThisWorkbook
- In the code window, type the
Workbook_Deactivate
event procedure:
Private Sub Workbook_Deactivate() MsgBox ThisWorkbook.Name & " deactivated" End Sub
To check if the event procedure working or not, switch to the Excel window and activate a different workbook. This action will trigger the Workbook_Deactivate
event procedure which displays a message box with BrainBell.xlsm deactivated
message: