Author: Oscar Cronquist Article last updated on August 31, 2021

This article explains how to set up a workbook so a macro is run every time you open the workbook. Excel Event code makes this possible and it is easier than you think.

Event code can run a macro based on a drop-down list, event code is VBA code stored in a worksheet module or workbook module. I will explain this later on in this article.

For example, you could have a macro that opens another workbook that you need, saving you time, making you happy, and feeling awesome.

1. Run a macro automatically when a specific workbook is opened

1.1 Create a macro

The following macro opens workbook Book1.xlsx in folder c:\temp

'Name macro
Sub Macro1

'Open a given workbook
Workbooks.Open ("c:\temp\Book1.xlsx")
End Sub

Back to top

1.2 Where to put the code?

Copy the macro code above and go to tab "Developer" on the ribbon. If it is missing search the internet for your Excel version and "Show developer tab".

Press with mouse on "Visual Basic" button to open the Visual Basic editor. Press with right mouse button on on your workbook in the Project Explorer window.

Press with mouse on "Insert" and then on "Module". This action adds a code module to your workbook.

Now paste the VBA code above to your code module.

Go back to Excel.

Back to top

1.3 How to run macro

Go to tab "Developer" and press with left mouse button on the "Macro" button. The following dialog box appears:

Press with mouse on "Run" button and the workbook is opened.

Back to top

1.4 Where to put event code?

Press Alt+F11 to open the VB Editor or go to tab "Developer" on the ribbon, press with left mouse button on "Visual Basic" button. Double press with left mouse button on "This Workbook", if you can't see it expand the list by press with left mouse button oning on the + sign.

Paste the following event code to the workbook module:

Private Sub Workbook_Open()
Macro1
End Sub

Go back to Excel. Save the workbook with file extension *.xlsm, this is important.

Close workbook and open it again. The workbook Book1.xlsx in folder c:\temp is now automatically opened. (If it exists..)

Back to top

2. Run a macro automatically when selecting a specific worksheet

Run a macro automatically when activating a specific worksheet

This section describes how to run a macro if a specific worksheet is selected (activated). Each worksheet in a workbook has a corresponding worksheet module that you can easily access, however, put only event code in these modules.

Note, put regular macros in regular modules.

2.1 Worksheet event code

Run a macro automatically when activating a specific worksheet access worksheet module

The following steps describe how to save event code to a specific worksheet.

  1. Press the right mouse button on the worksheet tab you want to edit, it is located at the left bottom of your Excel window.
  2. A pop-up menu appears, see the image above.
  3. Press with the left mouse button on "View Code" to open the corresponding worksheet module in Visual Basic Editor (VBE).
  4. Copy event code below.
  5. Paste to worksheet module.

Run a macro automatically when activating a specific worksheet worksheet module code

'Event code that runs when the user press with left mouse button ons on a specific worksheet tab
Private Sub Worksheet_Activate()

'Start macro named Macro2
Call Macro2
End Sub

Back to top

2.2 Macro code

Run a macro automatically when activating a specific worksheet

The following VBA code is a regular macro. it shows a message box containing "Hello world!". Here is how you store regular macros:

  1. Press Alt + F11 to open the Visual Basic Editor.
  2. Press the left mouse button on "Insert" on the top menu, see the image below.
  3. Press the left mouse button on "Module" to create a new module, they are located in the Modules folder shown in the image below.
  4. Copy code below.
  5. Paste to the module.
  6. Exit Visual Basic Editor.

Run a macro automatically when activating a specific worksheet module

Sub Macro2()

MsgBox "Hello world!"

End Sub

Back to top

3. Run macro when a specific cell is selected

Run macro when specific cell is selected

The image above demonstrates a macro that is run when a specific cell is selected on a specific worksheet. The example shown in the image above shows a message box containing text "Cell B2 is selected" when cell B2 is selected.

3.1 Worksheet code

The following steps describe how to save event code to a specific worksheet.

  1. Press the right mouse button on the worksheet tab you want to edit, it is located at the left bottom of your Excel window.
    Run macro when a specific cell is selected view worksheet module
  2. A pop-up menu appears, see the image above.
  3. Press with the left mouse button on "View Code" to open the corresponding worksheet module in Visual Basic Editor (VBE).
  4. Copy event code below.
  5. Paste to worksheet module.
    Run macro when a specific cell is selected
'Event code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'Interesect method returns a range object of a rectangular intersection of two or more cell ranges
If Not Intersect(Target, Range("B2")) Is Nothing Then

'Start macro named Macro3
Call Macro3

End If

End Sub

Back to top

3.2 Macro code

Run macro when specific cell is selected

The following VBA code is a regular macro. it shows a message box containing "Cell B2 is selected". Here is how you store regular macros:

  1. Press Alt + F11 to open the Visual Basic Editor.
  2. Press the left mouse button on "Insert" on the top menu, see the image below.
  3. Press the left mouse button on "Module" to create a new module, they are located in the Modules folder shown in the image below.
  4. Copy code below.
  5. Paste to the module.
  6. Exit Visual Basic Editor.

Run a macro automatically when activating a specific worksheet module

Sub Macro3()

MsgBox "Cell B2 is selected"

End Sub

Back to top

Note, save your workbook with file extension *.xlsm (macro-enabled workbook) to keep the code attached.