Events

 

Event Examples    |   Workbook Event

This chapter teaches you how to program a workbook event in Excel VBA. Events are actions performed by users, which trigger Excel VBA to execute a macro. First, we will look at some examples of events.

Event Examples

Below is a list of event examples.

- the user opens a workbook (workbook event).

- the user activates a worksheet (worksheet event).

- the user changes the content of a cell on the sheet (worksheet event).

- the user double clicks a cell (worksheet event).

Workbook Event

Workbook events are actions performed on the workbook (= your Excel file) which trigger Excel VBA to execute a macro. To create a workbook event, execute the following steps.

1. Launch the Visual Basic Editor.

2. Double Click on This Workbook in the Project Explorer (important!). The code window will appear showing you two drop-down lists.

3. Choose Workbook from the left drop-down list.

4. The right drop-down list shows you all the workbook events (don't worry, you will never use most of these events). Choose the Open event. Excel VBA automatically places a Sub procedure for you.

5. Now you can add a line which will only be executed by Excel VBA when you open the workbook! Add the line:

MsgBox "Good Morning"


Workbook Event Code

6. Close the Excel file (don't forget to save).

7. Open the Excel file and test the workbook event.

Result:

Workbook Event Result

Note: There is a chance your macro has not been executed because your security settings are not set up correctly. If so, go to Security Settings and click on: enable all macros.

Did you find this information helpful? Show your appreciation, vote for us.

Go to Top: Events    |    Go to Next Topic: Array

Learn more about events, Login to the right >>
100 easy to follow Excel VBA examples. Limited time-offer: $39.00 but only $29.00. Ends on 29th February.