Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Run a VBA Macro in Excel: Step-by-Step

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To run a VBA macro in Excel, you first need a workbook that contains macro code and a security setting that allows macros to execute. The quickest path is to open the workbook, enable macros if prompted, then press Alt+F8 (Windows) or Fn+Option+F8 (Mac) to open the Macro dialog, select the macro, and click Run. If the macro is attached to a button or shape, you can simply click that control. The steps below cover the full process from enabling the Developer tab to running, editing, and troubleshooting macros safely.

What You'll Need

  • Microsoft Excel for Windows or Mac (the desktop app, not Excel for the web, which cannot run VBA macros).
  • A workbook that already contains a macro, saved with the .xlsm or .xlsb extension, or your own macro code.
  • Permission to change macro security settings on your computer; some organizations lock these down via group policy.
  • Basic awareness that macros can run harmful code, so only enable macros from files you trust.

Step-by-Step

  1. Open the workbook that contains the macro. If the file came from the internet or an email attachment, Windows may block it. Before opening, right-click the file in File Explorer, choose Properties, and if you see an Unblock checkbox at the bottom, tick it and click OK. On Mac, use System Settings > Privacy & Security to allow the file if macOS blocks it.
  2. Enable macros when prompted. A yellow security bar may appear below the ribbon saying macros have been disabled. Click Enable Content. If you see a dialog asking whether to enable macros, choose Enable Macros only if you trust the source. If no prompt appears and macros still will not run, check Trust Center settings (see step 3).
  3. Check macro security settings if needed. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. The typical choices are Disable all macros with notification (recommended, prompts you each time), Disable all macros except digitally signed macros, and Enable all macros (not recommended). For most users, the notification option is the right balance. Labels can differ slightly by Excel version and region.
  4. Show the Developer tab (optional but useful). Go to File > Options > Customize Ribbon, tick Developer in the right-hand list, and click OK. On Mac, go to Excel > Preferences > Ribbon & Toolbar and tick Developer. This tab gives you quick access to the Macros button, Visual Basic editor, and macro security.
  5. Open the Macro dialog. On Windows, press Alt+F8. On Mac, press Fn+Option+F8 (or Option+F8 depending on your keyboard settings). You can also click Developer > Macros. The dialog lists macros available in the current workbook and, if you choose, in all open workbooks.
  6. Select the macro and click Run. If you do not see your macro, check the Macros in dropdown and switch to This Workbook or All Open Workbooks. Macros that require arguments will not appear in this list; those are usually run from a button or another macro.
  7. Alternatively, run a macro from a button or shape. If the workbook has a Form Control button or shape assigned to a macro, simply click it. To assign a macro to a shape yourself, right-click the shape, choose Assign Macro, pick the macro, and click OK.
  8. Run a macro from the Visual Basic Editor (for editing or stepping through code). Press Alt+F11 (Windows) or Fn+Option+F11 (Mac) to open the editor. In the Project Explorer, find your workbook, open its Modules folder, and double-click the module containing your code. Place the cursor inside the subroutine and press F5 to run it, or use the Run menu. This is also where you can step through code with F8 to debug.
  9. Save your work before and after running. Macros can change data irreversibly. Save a copy of the workbook first, especially if the macro deletes rows, formats sheets, or writes to other files. After a successful run, save again if you want to keep the results.

Common Mistakes

  • Using Excel for the web. The browser version cannot run VBA macros. Open the file in the desktop app instead.
  • Saving as .xlsx. A standard .xlsx file cannot store macros. If you record or write a macro, save as .xlsm (macro-enabled workbook) or .xlsb (binary workbook).
  • Ignoring the security bar. Many people close the yellow bar without clicking Enable Content, then wonder why nothing happens.
  • Looking in the wrong workbook. The Macro dialog defaults to macros in the active workbook. If your macro lives in a personal workbook or an add-in, change the Macros in dropdown.
  • Running a macro that expects arguments. Macros with parameters do not show in the Macro dialog. Run them from a button, another macro, or the Immediate Window in the editor.
  • Trusting unknown files. Enabling macros in a file from an untrusted source can install malware. Only enable macros in files you or your organization created or verified.

Tips

  • Use Alt+F8 often; it is the fastest way to see what macros are available in the open workbooks.
  • Record a macro with Developer > Record Macro to capture repetitive steps, then edit the generated code in the Visual Basic Editor to refine it.
  • Add a keyboard shortcut when recording (for example Ctrl+Shift+R) so you can run the macro without opening the dialog.
  • Keep macros in a dedicated module and give them descriptive names; this makes them easier to find in the Macro dialog.
  • Test macros on a copy of your data first, especially any macro that writes, deletes, or moves data.
  • If you share the workbook, tell recipients they must enable macros and that the file must stay in .xlsm format.

Troubleshooting

If the macro does not run, work through these checks in order, simplest first.

  1. Confirm macros are enabled. Look for the security bar and click Enable Content. If it does not appear, open Trust Center settings and confirm macro settings are not set to disable all macros without notification.
  2. Check the file format. If the workbook is .xlsx, macros cannot be stored or run. Save a copy as .xlsm and reopen it.
  3. Unblock the file. On Windows, right-click the file > Properties > tick Unblock > OK. On Mac, check System Settings > Privacy & Security for any block message and allow the file.
  4. Verify the macro exists and the name is correct. Open the Macro dialog and switch the Macros in dropdown to All Open Workbooks. If the macro still does not appear, open the Visual Basic Editor and confirm the code is in a standard module, not a sheet module, and that the subroutine is not marked Private.
  5. Look for compile or runtime errors. In the editor, use Debug > Compile VBAProject. Fix any highlighted errors. When running, if an error dialog appears, click Debug to see the failing line.
  6. Check for disabled add-ins or protected view. Files opened from the internet may open in Protected View, which blocks macros. Click Enable Editing on the yellow bar first, then enable macros.
  7. Restart Excel. Close and reopen Excel, then try again. If the problem persists, restart the computer.
  8. Ask your administrator. In managed environments, group policy can block macros entirely. If Trust Center settings are greyed out, contact your IT administrator.

Warning: Do not disable macro security globally or enable all macros as a permanent fix. That leaves your computer exposed to malicious code in any workbook you open. Use the notification setting and enable macros only for trusted files.

Frequently Asked Questions

Can I run a VBA macro in Excel for the web?

No. Excel for the web does not support VBA macros. You need the desktop version of Excel for Windows or Mac to run them.

Why does the Macro dialog show no macros?

Either the active workbook contains no macros, the macro is Private or requires arguments, or macros are disabled by security settings. Switch the Macros in dropdown to All Open Workbooks and check Trust Center settings.

What file extension do I need for a macro-enabled workbook?

Use .xlsm for a standard macro-enabled workbook or .xlsb for a binary workbook. A regular .xlsx file cannot store VBA code.

How do I run a macro with a keyboard shortcut?

When recording a macro, assign a shortcut key in the Record Macro dialog. You can also assign one later via Developer > Macros > Options for an existing macro.

Is it safe to enable macros in a workbook I downloaded?

Only if you trust the source. Macros can perform any action you can, including deleting files and changing system settings. When in doubt, do not enable them; ask the sender to confirm the file is safe.

Can a macro run automatically when I open a workbook?

Yes, if the workbook contains a subroutine named Auto_Open or code in the Workbook_Open event. These run when the file opens, provided macros are enabled. This is another reason to enable macros only for trusted files.