This guide shows you how to automatically run Excel VBA code when a workbook opens using the Workbook_Open event. No additional tools are needed—just the built-in VBA editor in Excel.
What You'll Need
- Microsoft Excel (any recent version that supports VBA, such as Excel 2010 or later)
- A workbook where you want the code to run on open
- Basic familiarity with opening the VBA editor (Alt+F11)
Step-by-Step
- Open the Excel workbook where you want the code to run when it opens.
- Press Alt + F11 to open the VBA editor.
- In the Project Explorer pane (usually on the left), locate your workbook under VBAProject (YourWorkbookName).
- Double-click ThisWorkbook under that project to open its code window.
- In the code window for ThisWorkbook, locate the two dropdown menus at the top: the left one says (General) and the right one says (Declarations).
- Click the left dropdown and select Workbook from the list.
- Click the right dropdown and select Open from the list. This will automatically generate the
Private Sub Workbook_Open()andEnd Sublines. - Between these lines, add the VBA code you want to run when the workbook opens. For example:
Private Sub Workbook_Open() MsgBox "Welcome back! The workbook has opened.", vbInformation ThisWorkbook.Sheets("Dashboard").Range("A1").Value = Now End Sub - Close the VBA editor by clicking the X or pressing Alt + Q.
- Save the workbook as a macro-enabled file: click File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown, and save.
- Close and reopen the workbook to test. You should see the message box and any other code execute automatically.
Common Mistakes
- Writing the code in a regular module instead of
ThisWorkbook—the Workbook_Open event only works in the ThisWorkbook code window. - Forgetting to save the file as
.xlsm; if saved as.xlsx, VBA code is stripped and won’t run. - Having macros disabled in Excel’s Trust Center settings, which prevents any VBA from running on open.
- Using
ActivateorSelectunnecessarily in the code, which can cause errors if the expected sheet or range isn’t active. - Adding code that takes too long to run or shows userforms before Excel is fully initialized, leading to instability.
Tips
- Use
Application.ScreenUpdating = Falseat the start andApplication.ScreenUpdating = Trueat the end of your code to prevent screen flicker during initialization. - Include error handling with
On Error GoTo ErrHandlerto avoid silent failures that make it seem like the event didn’t trigger. - Test your code by calling it manually from the VBA editor (place cursor inside the sub and press F5) to verify logic before relying on the open event.
- Avoid placing heavy data processing or external calls (like web queries) directly in Workbook_Open; instead, use a
Application.OnTimedelay to run them a second or two after opening. - If you want the code to run only once per session, consider using a public variable or storing a flag in a hidden name or custom document property.
Troubleshooting
- Code doesn’t run when opening: First, confirm macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure Enable all macros (for testing) or Disable all macros with notification is selected. If you see a security warning when opening, click Enable Content.
- Still not running: Verify you saved the file as
.xlsm. Open the file, press Alt+F11, and check that your code is still inThisWorkbookunder the correct event. - Error occurs on open: Hold down the Shift key while opening the workbook to bypass auto-run macros. Then open the VBA editor, debug the code, and fix any issues (e.g., referencing a sheet that doesn’t exist).
- Code runs but does nothing: Add a simple
MsgBoxat the start to confirm the event is firing. If you see the box, the issue is in your logic, not the event trigger. - Excel crashes on open: Again, use Shift+Open to disable macros, then remove or comment out recent code. Common causes include infinite loops, invalid object references, or attempting to access protected workbooks.
Frequently Asked Questions
Can I use Workbook_Open in a personal macro workbook?
Yes, but the Personal Macro Workbook is hidden and opens silently; code in its ThisWorkbook module will run when Excel starts, not when a specific user workbook opens.
Will Workbook_Open trigger if I open the workbook via Power Query or Power Automate?
No. These methods open the workbook in a background or headless instance where VBA events typically do not fire.
Can I cancel the workbook opening from within Workbook_Open?
Not directly. You can close the workbook using ThisWorkbook.Close SaveChanges:=False, but the user will still see it open briefly before closing.
Is there a way to run code only the first time the workbook is opened each day?
Yes. Store the last run date in a named range, hidden worksheet, or document property, and compare it to the current date at the start of Workbook_Open.
Does Workbook_Open run when creating a new workbook from a template?
Yes. When a new workbook is created based on a template (.xltx or .xltm) that contains the Workbook_Open event, the code runs as part of the new workbook’s initialization.
Can I use Workbook_Open to change Excel’s application-level settings?
Yes, but be cautious. Changes like Application.EnableEvents = False or modifying Calculation mode affect all open workbooks and should be reset in Workbook_BeforeClose or similar events to avoid side effects.