Yes, you can make an Excel VBA macro run when a workbook opens—this is done by attaching the macro to the Workbook_Open event in the VBA editor. You simply paste your code into the ThisWorkbook module. Below is a step-by-step guide to setting this up correctly, covering what you’ll need, how to avoid common mistakes, and how to troubleshoot if it doesn’t work.

What You'll Need

  • An Excel workbook with macros enabled (ensure the file extension is .xlsm, not .xlsx).
  • The Developer tab visible in Excel’s ribbon (this is where you access VBA).
  • A basic understanding of VBA syntax—at least enough to know how to type Sub and End Sub.
  • Administrator or full permissions on your computer (for security reasons, Excel restricts certain actions without proper access).

Step-by-Step

  1. Open your workbook. Ensure it’s in .xlsm format—this is required for macros to run. If it’s in .xlsx, save it as a macro-enabled workbook:
    1. Go to File > Save As.
    2. In the Save as type dropdown, choose Macro-Enabled Workbook (*.xlsm).
    3. Click Save.
  2. Enable the Developer tab (if not already visible):
    1. Right-click anywhere on the ribbon and select Customize the Ribbon.
    2. In the Excel Options dialog box, check the box next to Developer under Main Tabs.
    3. Click OK.

    Note: If the Developer tab isn’t visible after this, restart Excel.

  3. Open the VBA editor:
    1. Click the Developer tab on the ribbon.
    2. Click Visual Basic (or press Alt + F11 as a shortcut).
    3. The VBA editor will open, showing the Project Explorer on the left.
  4. Insert a new module or use ThisWorkbook:
    1. In the Project Explorer, locate your workbook’s name under VBAProject.
    2. Double-click ThisWorkbook (this is a built-in module where workbook events like Open are handled).
    3. If ThisWorkbook doesn’t exist, right-click in the Project Explorer under your workbook’s name and select Insert > Module. Then, manually type the following code:
    Sub Workbook_Open()
        ' Your code here
        MsgBox "This macro runs when the workbook opens!"
    End Sub

    Note: The correct event handler is Workbook_Open (not Workbook_Activate, which runs when a sheet is activated).

  5. Add your macro code:
    1. Replace the placeholder MsgBox with your actual code. For example, to auto-filter data when the workbook opens:
    2. Sub Workbook_Open()
          Dim ws As Worksheet
          Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace "Sheet1" with your sheet name
          ws.Range("A1:D100").AutoFilter
      End Sub
  6. Save the workbook:
    1. Close the VBA editor (Alt + Q or click the X in the top-right corner).
    2. Save the workbook again—this ensures the macro is linked to the file.
  7. Test the macro:
    1. Close and reopen the workbook. Your macro should now run automatically.
    2. If you see an error message, check the Immediate Window (Ctrl + G in the VBA editor) for details.

Common Mistakes

  • Using the wrong event handler: Many beginners confuse Workbook_Open with Workbook_Activate or Sheet_Activate. The first runs when the workbook opens, while the others trigger when a specific sheet is selected.
  • Saving as a non-macro workbook (".xlsx"): Macros won’t run unless the file is saved as .xlsm. Always double-check the file extension.
  • Forgetting to enable macros: Even if the file is .xlsm, Excel may block macros by default. See the Security section below for how to enable them.
  • Typing errors in the code: A single misspelled keyword (e.g., Subbook_Open instead of Workbook_Open) will cause the macro to fail silently.
  • Not testing after saving: Always reopen the workbook to confirm the macro runs as expected.

Tips

  • Use On Error Resume Next for robustness: If your macro might fail (e.g., referencing a non-existent sheet), add error handling to prevent crashes:
    Sub Workbook_Open()
        On Error Resume Next
        MsgBox "Auto-run successful!"
        On Error GoTo 0
    End Sub
  • Log actions to a worksheet: To debug or track what happens, write output to a hidden sheet:
    Sub Workbook_Open()
        Dim wsLog As Worksheet
        Set wsLog = ThisWorkbook.Sheets("Log")
        wsLog.Cells(1, 1).Value = "Workbook opened at: " & Now()
    End Sub
  • Enable macros via Trust Center: If macros are blocked, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros (not recommended for security-sensitive workbooks).
  • Avoid long-running macros: Complex macros that take time to execute may cause Excel to appear unresponsive. Split them into smaller subroutines or run them in the background using Application.ScreenUpdating = False.

Troubleshooting

If your macro isn’t running when the workbook opens, follow this order:

  1. Check macro security settings:
    1. Open Excel and go to File > Options > Trust Center > Trust Center Settings > Macro Settings.
    2. Ensure Enable all macros is selected. If you’re unsure, choose Disable all macros with notification and manually enable them when prompted.
  2. Verify the file extension:
    1. Right-click the workbook file in Windows Explorer and check the extension (it should end in .xlsm).
    2. If it’s .xlsx, save it as .xlsm as described in Step 1.
  3. Check for broken references:
    1. Open the VBA editor (Alt + F11).
    2. Go to Tools > References and ensure all necessary libraries (like Microsoft Excel Object Library) are checked.
  4. Test in Safe Mode:
    1. Close Excel completely.
    2. Press Windows + R, type excel /safe, and press Enter. This disables add-ins and macros.
    3. Open your workbook. If the macro runs in Safe Mode, a third-party add-in is likely interfering. Disable add-ins via File > Options > Add-ins.
  5. Check for VBA errors:
    1. Open the VBA editor and look for red squiggly lines underlining errors.
    2. Press F7 to check syntax or Ctrl + G to open the Immediate Window and run Debug.Print statements for debugging.
  6. Recreate the ThisWorkbook module:
    1. If the module is corrupted, delete it from the Project Explorer.
    2. Right-click your workbook’s name > Insert > Module, then manually type Workbook_Open as shown in Step 4.
  7. Repair the workbook:
    1. Go to File > Open and browse to your workbook.
    2. Click the dropdown next to Open and select Open and Repair.
    3. If the workbook repairs successfully, try running the macro again.

Frequently Asked Questions

Why doesn’t my macro run when I open the workbook?

Check the following:

  • The workbook is saved as .xlsm, not .xlsx.
  • Macros are enabled in Excel’s Trust Center settings.
  • The code is pasted into ThisWorkbook, not a regular module.
  • There are no syntax errors in your VBA code.
If all else fails, test in Safe Mode to rule out add-in conflicts.

Can I run a macro when a specific sheet is opened?

Yes, but you’ll need to use the Sheet_Activate event. Insert a module in the sheet’s code (right-click the sheet tab > View Code) and add:

Private Sub Worksheet_Activate()
    MsgBox "This sheet was activated!"
End Sub
Note the Private keyword—this makes the event specific to the sheet.

How do I stop a macro from running on open?

Delete the Workbook_Open subroutine from the ThisWorkbook module. Alternatively, comment out the code by adding an apostrophe (') at the start of each line.

Will my macro run if I email the workbook?

Yes, but recipients must have macros enabled in their Excel settings. You can include instructions like "Enable macros in Trust Center" in a readme sheet to guide them.

Can I run a macro on open without saving as .xlsm?

No. Excel requires the .xlsm extension to store and execute macros. If you must avoid this, consider:

  • Using a button to trigger the macro manually.
  • Embedding the macro in a VBA project (not recommended for sharing).

Why does my macro run once but not on subsequent opens?

This often happens if the macro modifies the workbook’s structure (e.g., deleting sheets) or if there’s an error that stops execution. Add On Error Resume Next at the start of the subroutine to suppress errors and prevent crashes.

How do I debug a macro that fails silently?

Use the VBA debugger:

  1. Place a breakpoint by clicking the left gutter next to the line number in the code.
  2. F5 to run the macro in debug mode.
  3. When it stops, check the Immediate Window (Ctrl + G) and use Debug.Print statements to log variables.
  4. Press F8 to step through the code line by line.