Run a Macro When Excel Workbook Opens: VBA Guide
In this article
To run a macro automatically when a workbook opens, put your code in the Workbook_Open event procedure inside the ThisWorkbook module, then save the file as an .xlsm (macro-enabled) workbook and enable macros when prompted. That is the whole mechanism: Excel fires Workbook_Open every time the file is opened normally, and any code you place there runs before you can interact with the sheet.
What You'll Need
- Microsoft Excel on Windows or Mac (the VBA editor is included in both; the menu labels differ slightly between versions and platforms).
- A workbook you can save as
.xlsmor.xlsb. Standard.xlsxfiles cannot store macros. - Macro security set to allow macros, at least for trusted locations or signed files. If macros are disabled,
Workbook_Opennever runs. - Basic familiarity with opening the VBA editor (Alt+F11 on Windows; on Mac, Tools > Macro > Visual Basic Editor from the Excel menu).
Step-by-Step
- Open the workbook you want to automate. Work on a copy first if the file matters, because you will be changing its file type and adding code.
- Open the VBA editor. On Windows press Alt+F11. On Mac, go to Tools > Macro > Visual Basic Editor. A separate editor window appears.
- Find the Project Explorer. It is usually on the left. If it is hidden, use View > Project Explorer. You will see a tree with your workbook name and a
VBAProjectnode beneath it. - Double-click
ThisWorkbook. This is a special module tied to the workbook itself, not a standard module. Code placed in a standard module will not fire on open; it must be inThisWorkbook. - Set the code window to show events. At the top of the code window are two dropdowns. In the left dropdown choose
Workbook. Excel inserts an emptyPrivate Sub Workbook_Open()andEnd Subfor you. - Write your macro between those lines. For example, to jump to a summary sheet and show a message:
Private Sub Workbook_Open() Sheets("Summary").Activate MsgBox "Welcome. Data was last refreshed on " & Format(Now, "yyyy-mm-dd") End SubKeep the first line exactly asPrivate Sub Workbook_Open(). Renaming it, adding arguments, or moving it to another module breaks the trigger. - Keep the code short and safe. Anything that deletes rows, overwrites cells, or sends email should be guarded. A simple pattern is to check a cell flag before acting, so the action runs only once per day or once per session.
- Save as a macro-enabled workbook. Use File > Save As and choose Excel Macro-Enabled Workbook (
.xlsm). If you keep.xlsx, Excel warns that macros cannot be saved and strips the code. - Close and reopen the file to test. When Excel shows the yellow security bar or a dialog about macros, click Enable Content (or Enable Macros). If you click Disable, the code stays dormant and nothing happens.
- Confirm the macro ran. You should see your message box, sheet switch, or other effect immediately on open. If nothing happens, work through the Troubleshooting section below.
Common Mistakes
- Putting the code in a standard module. A
Sub Auto_Open()in a normal module can work in some cases, butWorkbook_OpeninThisWorkbookis the reliable, documented event. Use that. - Saving as
.xlsx. The code is silently discarded. Always use.xlsmor.xlsb. - Renaming the procedure.
Workbook_Openis case-insensitive but must be spelled exactly and take no arguments.Workbook_Open2orWorkbook_Open(ByVal x)will not fire. - Macros disabled by security settings. Trust Center settings can block all macros without prompting. Check File > Options > Trust Center > Trust Center Settings > Macro Settings (Windows) or Excel > Preferences > Security (Mac).
- Assuming it runs when opened from another macro or a link.
Workbook_Opendoes not fire when a workbook is opened programmatically withApplication.EnableEvents = False, or when opened via certain automation paths. Test by double-clicking the file in Explorer or Finder. - Leaving
EnableEventsoff. If your macro turns events off and errors before turning them back on, future opens may behave oddly. Always restore it in an error handler. - Long or risky operations on open. If the macro takes minutes or crashes, users may think the file is broken. Keep open-time work minimal and move heavy processing to a button.
Tips
- Use
ThisWorkbook.PathandThisWorkbook.Namerather than hard-coded file paths so the macro keeps working if the file moves. - Wrap risky code in
On Error GoToand show a friendly message instead of a VBA error dialog. - If you need to run code only once per day, store a date stamp in a hidden sheet or a custom document property and compare it on open.
- For shared files, avoid actions that change the active sheet for other users unexpectedly; a status message is usually safer than a forced sheet switch.
- Sign your VBA project with a digital certificate if you distribute the workbook, so users can trust it without lowering security globally.
- Keep a backup copy of the original file before adding macros, and test on a copy before rolling out to colleagues.
Troubleshooting
Nothing happens on open. First check that the file is saved as .xlsm and that the code is in ThisWorkbook, not a standard module. Then confirm macros are enabled: look for the security bar and click Enable Content. If your organisation blocks macros by policy, you may need a trusted location or a signed project.
You get a compile error. The most common cause is a typo in the procedure name or a missing End Sub. In the editor, use Debug > Compile VBAProject to catch syntax errors before reopening.
The macro runs but the wrong sheet is active. Sheet names in code must match the tab names exactly, including spaces. Use the sheet's code name (visible in the Properties window) if you want it to survive renaming.
It works for you but not for a colleague. Their Excel may have macros disabled, or the file may have been saved as .xlsx after editing. Send the .xlsm file and ask them to enable content. If they are on Mac and you are on Windows, check that any Windows-only references (such as certain ActiveX controls) are removed.
Excel opens in Safe Mode or the file opens read-only. Safe Mode disables macros. Close Excel fully, reopen normally, and check whether the file is marked as blocked (right-click the file > Properties > Unblock on Windows).
You want to stop the macro temporarily. Hold Shift while opening the workbook; this suppresses Workbook_Open in most Excel versions. Alternatively, disable macros for that session.
Frequently Asked Questions
Can I run a macro on open without enabling macros?
No. VBA code only runs when macros are enabled. If macros are disabled by policy or by the user, Workbook_Open will not execute. There is no supported way to bypass that security setting.
Does Workbook_Open run when the file is opened from a hyperlink or another workbook?
It runs when Excel opens the workbook normally, including double-clicking the file. It does not run if events are disabled by the calling code, or if the file is opened in a way that suppresses events. Test the exact method your users will use.
What is the difference between Auto_Open and Workbook_Open?
Auto_Open is an older macro name that can sit in a standard module. Workbook_Open is the modern event procedure tied to the workbook. Prefer Workbook_Open; it is more predictable and works with event handling.
Can I run code when the workbook closes as well?
Yes. In the same ThisWorkbook module, choose Workbook_BeforeClose from the right-hand dropdown and add your cleanup code there. Keep it short, because users expect the file to close promptly.
Will the macro run on Excel for Mac?
Yes, VBA and Workbook_Open work in Excel for Mac. Menu locations and some file-path behaviour differ, and Windows-only APIs are not available. Test on the platform your users actually use.
How do I stop the macro from running every time?
Hold Shift while opening the file to skip the open event, or temporarily disable macros. For a permanent change, remove or comment out the code in ThisWorkbook and save.