Automatically Run a Macro When a Workbook is Opened

Add to Favorites
Author:

How to make a macro run automatically after a workbook is opened and before anything else happens.

You need this macro code:

Private Sub Workbook_Open()

'simple message box that will pop-up and say "Hi!" to show you that the macro is working

MsgBox "Hi!"

End Sub

The trick is that you must put this macro code into ThisWorkbook

Steps to Make a Macro run When a Workbook is Opened

  1. Hit Alt + F11 on the keyboard to get to the Macro editor.
  2. Then, instead of creating a Module, which is what we usually do, we double-click where it says ThisWorkbook:
  3. In the window that appears, go to the left drop down menu and select Workbook:
  4.  If the default text for Open doesn't appear, then look to the right drop down menu and select Open from the list:
  5. Type whatever macro code you want to run in the middle and that's it!

Make sure to download the accompanying workbook to see this macro in action.

Macro Might Not Run Automatically If...

Doing this does not mean that a macro will always run when a workbook is opened.  Due to security features in Excel, many workbooks will have macros turned off by default.  In order for this to run, the macros have to be enabled.

You may see a warning when you open a workbook with a macro in it and you will then have to enable the macros from that warning.

However, some people will have all macros turned off without notification, which means they will not see an option to turn them on.

All of this just means that you cannot plan for all of your users to have macros turned on by default and, as such, you shouldn't have mission-critical macros run in this manner.

In another tutorial, I'll show you how to make users enable their macros in order to view the workbook so that a problem like this can be avoided.


Downloadable Files: Excel File

Question? Ask it in our Excel Forum


Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons 50+ Hours of Instruction 200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Similar Content on TeachExcel
Run a Macro when a User Does Something in the Workbook in Excel
Tutorial: How to run a macro when a user does something within the Workbook in Excel, such as openi...
Run a Macro when a User Does Something in the Worksheet in Excel
Tutorial: How to run a macro when a user does something in the worksheet, everything from selecting ...
Automatically Run a Macro so Many Seconds, Minutes, or Hours After an Excel Workbook has been Opened
Macro: Run a macro after a certain amount of time has passed since the Excel workbook was ope...
Run a Macro When a Specific Cell Changes in Excel
Tutorial: Run a macro in Excel when a specific cell is changed; this also covers when a cell within...
Automatically Run a Macro at a Certain Time - i.e. Run a Macro at 4:30PM every day
Macro: Automatically run an Excel macro at a certain time. This allows you to not have to worry a...
VBA IF Statement in Excel Macros
Tutorial: IF statements allow you to make decisions in your Macros and VBA in Excel. An IF statement...
Tutorial Details
Downloadable Files: Excel File
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course