10/03/2012

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.

 
Copyright 2009 360 graus