The issue isn’t just that your Excel macro won’t run—it’s likely one of these three things: a missing or incorrect reference to external libraries, a syntax error in your code, or a setting that blocks execution (like VBA disabled in Trust Center). Fix it with these steps, and you’ll have it working again in minutes.

What You'll Need

  • A working copy of Microsoft Excel (365 or 2019+ recommended, as older versions may lack newer macros)
  • VBA editor access (if using Excel’s built-in editor, no extra tools needed; if debugging, an IDE like Visual Studio Code with a VBA extension)
  • If you get errors, a journal or notepad to log line numbers and error messages
  • Admin rights to install or update macros (if your version blocks them via security settings)

Step-by-Step

  1. Check if macros are enabled
    1. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Ensure "Enable all macros" (or "Enable this workbook only") is selected, depending on your security level.
    2. If you’re on a corporate or restricted system, check with IT—some organizations block macros for security.
  2. Verify the code opens correctly
    1. Press Alt + F11 to open the VBA editor. If you see a dialog asking whether to open the workbook’s macro project, click Yes.
    2. In the Project Explorer window, ensure your macro is in the correct module (e.g., ThisWorkbook for workbook-level macros). If it’s missing, right-click the module name and select Insert > Module to create one.
  3. Test the macro in a blank workbook
    1. Create a new Excel file (Ctrl+N). In VBA, run your macro again. If it still fails, the issue isn’t the workbook itself but the macro’s design.
    2. Run the macro directly via F5 in the VBA editor to catch immediate errors (e.g., missing variables, syntax mistakes).
  4. Check for runtime errors
    1. If the macro crashes, note the error message (e.g., "Compile error" or "Runtime error 1004"). Common causes:
      • Missing references: Right-click the module > References > Check if libraries like Microsoft Excel Object Library are listed. If not, enable them via Tools > References in VBA.
      • Uninitialized variables: Ensure all variables are declared (e.g., Dim myRange As Range) before use.
      • Worksheet protection: Check Review > Unprotect Sheet to confirm the sheet isn’t locked.
  5. Update and clean macros
    1. Close and reopen Excel. Sometimes, corrupted macros cause silent failures.
    2. For older macros, convert them to VBA 6.0 compatibility if using Excel 2016 or earlier (right-click module > Tools > Convert to VBA Project).
    3. Enable the "Ignore errors" checkbox in VBA’s ThisWorkbook module if you’re debugging complex errors (temporarily).

Common Mistakes

  • Macro references missing: Forgetting to add external libraries (e.g., Microsoft Scripting Runtime) via VBA > References. This often causes "Type mismatch" errors.
  • Case-sensitive paths: In VBA, file paths like "C:\Users\Name\Documents\file.xls" must match exactly. A typo here triggers runtime failures.
  • Worksheet events overwritten: If you’ve defined a Worksheet_Change event in multiple modules, the last one overwrites previous code. Use Private Sub Worksheet_Change(ByVal Target As Range) with a scope check.
  • Excel version incompatibility: Macros written for Excel 2016 may fail in 365 due to new security features (e.g., Application.DisplayAlerts behaves differently). Test in the target version first.

Tips

  • Use debugging shortcuts: F8 steps through code line by line; Shift+F8 continues past errors. Set breakpoints by clicking the left gutter.
  • Test in a copy: Duplicate your workbook, run the macro there, and compare logs. Isolation reveals whether the error is workbook-specific.
  • Check event triggers: If macros run on button clicks or user actions, ensure the event is properly tied to the UI (e.g., a Developer > Insert > Button in Excel).
  • Enable verbose errors: In VBA, set Option Explicit at the top of your module to catch undeclared variables early. Also, add a On Error Resume Next wrapper for targeted debugging.
  • Review the macro’s age: Very old macros (pre-2003) may lack modern syntax (e.g., For Each instead of loops). Convert them gradually.

Troubleshooting

If none of the steps above work, try this escalated approach:

  1. Restart Excel and your PC: Sometimes, a simple reboot clears latent errors (e.g., missing DLLs or corrupted cache). For Excel 365, restart in safe mode (Ctrl+Alt+Del > Task Manager > End Task > Restart Excel with Shift pressed).
  2. Reinstall VBA editor
    1. In VBA, go to Tools > Import/Export > Export VBA Project to File to save your macros. Delete the VBA project folder in C:\Users\YourName\AppData\Local\Microsoft\Excel\Excel (Windows) and reinstall Excel.
    2. After reinstalling, import your macros again to restore them.
  3. Update Excel and Windows
    1. Ensure Excel and Windows 10/11 are updated via Settings > Windows Update. Outdated versions may lack compatibility with modern macros.
    2. For Excel 365, check cloud updates in File > Account > Update Options > Update Now.
  4. Check for macro virus alerts
    1. If Excel shows a warning like "This workbook contains macros that may be unsafe," click Enable Content only if it’s from a trusted source. This bypasses security but isn’t recommended for sensitive workbooks.
    2. For corporate systems, contact IT—some blocks macros completely for security policies.
  5. Use a cleaner method: Record a macro
    1. If debugging is too slow, record a macro instead of editing it directly:
      1. Go to Record Macro in the Developer tab (enable it via File > Options > Customize Ribbon > Developer).
      2. Run your task, then stop recording. The macro may be simpler than the original.
    2. Compare the recorded macro with the original for errors.

Frequently Asked Questions

Why does my macro fail when I open the workbook but runs in VBA?

The issue is often a missing reference or event trigger. In VBA, macros run in isolation, but when opened in Excel, they must respect workbook-level settings (e.g., workbook protection) or worksheet events. Check if the macro uses Worksheet_Activate or requires the workbook’s active state.

How do I fix a "Runtime error 91" (Object variable or With block not set)?

This error occurs when Excel expects an object (e.g., Worksheet) but it’s null or uninitialized. Solutions:

  • Add a scope check: If TypeName(ActiveSheet) = "Worksheet" Then ... End If
  • Declare all objects explicitly (e.g., Dim ws As Worksheet)
  • Use Application.DisplayAlerts = False before critical operations to suppress prompts that might trigger errors.

Why does my macro work in Excel 2019 but not 365?

Excel 365 enforces stricter security:

  • Macros in 365 may be blocked unless explicitly enabled via Trust Center
  • Newer features (e.g., Application.DisplayAlerts behavior) differ between versions
  • VBA projects in 365 use a newer engine—some older macros fail to load. Test in both versions first.
For compatibility, use Option Explicit and avoid deprecated syntax like Range("A1").Select.

How do I debug a macro that doesn’t show errors in Excel?

Macros may silently fail when:

  • The code runs without errors but corrupts data (e.g., overwrites a range). Test in a copy.
  • Dependencies (e.g., external methods) fail. Add On Error Resume Next around them.
  • The macro triggers a UI event that crashes Excel. Use Application.ScreenUpdating = False before critical steps.
For hidden issues, use Debug.Print or a notepad to log outputs.

Is there a way to run macros without VBA?

Yes, use:

  • Excel’s built-in features: Use Data > Get Data > From File for simple transformations.
  • Power Query: Drag-and-drop data loading with no code.
  • Macro recipes: In VBA, repeat simple tasks in a loop (e.g., For i = 1 To 100: Range("A1").Value = "Text" Next).
For complex logic, VBA remains the only option.

How do I prevent macros from being blocked by Excel’s security?

If you can’t enable macros:

  • Use a trusted file location: Save your workbook in a shared drive or OneDrive (if enabled).
  • Create a shortcut to a trusted location: Move the macro-enabled file to your desktop and create a shortcut to it.
  • Ask IT to whitelist your macros: Some corporate systems allow exceptions for specific users.
For personal use, enable macros via Trust Center > Macro Settings.