Creating workbook-related events
The Open
event is one of many workbook-related events. In this recipe, we will be creating more workbook-related events.
Getting ready
Make sure that Events.xlsm
is still open. Sheet1 should be active. Press Alt + F11 to switch to the VBA Editor, and then double-click the ThisWorkbook
object to activate the code window.
How to do it…
Let's create three workbook-related events:
- If we can welcome users when they open a file, we can greet them when they leave. In the top-right corner of the code window, select the
BeforeClose
event from the drop-down list:A new procedure will be created in the code window:
Private Sub Workbook_BeforeClose(Cancel As Boolean) End Sub
- Insert the next line of code in the procedure:
Private Sub Workbook_BeforeClose(Cancel As Boolean) MsgBox "Goodbye - have a nice day" End Sub
- After saving the file, close it. A message...