Events and Event Procedures were introduced in Excel97. Earlier versions of Excel do not support events. An Event is an action initiated either by user action or by other VBA code.
Workbook Open Event
- Open the Visual Basic Editor. (Shortcut- F11)
- Double click on This Workbook in the Project Explorer.
- Choose Workbook from the left drop-down list. Choose Open from the right drop-down list.
- Add the following code line to the Workbook Open Event:
MsgBox “Welcome to VBA World!!!”
- Save, close and reopen the Excel file.
You will get Message Box Saying “Welcome to VBA World !!! “ at the very beginning once you will open the workbook.
Example to create follow-up worksheet on regular basis
Private Sub Workbook_Open()
Do Until ActiveCell.Value = “”
If (ActiveCell.Value = Date) Or (ActiveCell.Value = Date + 1) Then
ActiveCell.Interior.ColorIndex = 3
ActiveCell.Font.Bold = True
ActiveCell.Offset(0, 1).Value = “Follow Me”
Example: Worksheet Change Event
Code added to the Worksheet Change Event will be executed by Excel VBA when you change a cell on a worksheet.
- Open the Visual Basic Editor.
- Double click on a sheet (for example Sheet1) in the Project Explorer.
- Choose Worksheet from the left drop-down list. Choose Change from the right drop-down list.
- The Worksheet Change Event listens to all changes on Sheet1.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value > 80 Then MsgBox “Goal Completed”