Running a macro in Excel executes a recorded or written sequence of actions automatically. You can run a macro from the Ribbon, a keyboard shortcut, a button, or the Visual Basic Editor. This guide covers the fastest methods and what to do when a macro won’t run.
What You’ll Need
- A workbook that contains a macro (either recorded or written in VBA).
- The file must be saved with a macro-enabled extension, such as
.xlsmor.xlsb(not the default.xlsx). - Macros must be enabled in Excel. If you see a yellow security bar, click Enable Content.
- If the macro is in a different file, that file must be open in Excel.
Step-by-Step
- Open the workbook that contains the macro. If the macro is stored in your Personal Macro Workbook (
PERSONAL.XLSB), it is available in every workbook, but the file itself may be hidden. - Enable macros if prompted. When you open the file, look for a yellow bar under the Ribbon. Click Enable Content. If macros are disabled by policy, you may need to change Trust Center settings (see Troubleshooting).
- Open the Macro dialog box. Go to the View tab on the Ribbon. In the Macros group, click the Macros button (or press Alt+F8 on Windows, Option+F8 on Mac).
- Select the macro you want to run. The dialog box lists all available macros. If the macro is in a different workbook, use the Macros in dropdown to choose that workbook.
- Click Run. Excel executes the macro. If the macro requires input (like a range or a number), a prompt will appear.
- To run a macro with a keyboard shortcut: If you assigned a shortcut (e.g., Ctrl+Shift+M) when recording, just press it. To assign or change a shortcut later, open the Macro dialog, select the macro, click Options, and enter a new shortcut key.
- To run a macro from a button: If you added a button (Form Control or ActiveX) on a worksheet, simply click it. To create a button, go to Developer > Insert, choose a button, draw it on the sheet, and assign the macro.
- To run a macro from the Quick Access Toolbar: Click the small arrow at the top of the Excel window, choose More Commands, select Macros from the “Choose commands from” list, pick your macro, click Add, then OK. Now click that icon to run the macro.
- To run a macro from the Visual Basic Editor: Press Alt+F11 to open the VBA editor. In the Project Explorer, find the module containing the macro. Place your cursor anywhere inside the macro code, then press F5 to run it. This is useful for debugging.
Common Mistakes
- Forgetting to enable macros: If you don’t click “Enable Content,” the macro won’t run. The file may open read-only or with macros disabled.
- Saving as .xlsx: If you save a macro-enabled workbook as a regular Excel file, the macro is deleted. Always save as Excel Macro-Enabled Workbook (*.xlsm).
- Running the wrong macro: The Macro dialog lists all open workbooks’ macros. Check the “Macros in” dropdown to ensure you’re not running a similarly named macro from another file.
- Using relative vs. absolute references incorrectly: If your macro was recorded with relative references, it will run relative to the active cell. If you expected it to affect a fixed range, it may target the wrong cells.
- Macro security settings: If macros are disabled entirely, you won’t see the yellow bar. You need to adjust Trust Center settings (see below).
Tips
- Assign a keyboard shortcut that doesn’t conflict: Use Ctrl+Shift+letter (e.g., Ctrl+Shift+R) to avoid overriding built-in shortcuts like Ctrl+C.
- Test on a copy: Before running a macro on important data, save a copy of the workbook. Macros cannot be undone with Ctrl+Z.
- Use the Step Into feature: In the Macro dialog, select the macro and click Step Into to run it line by line in the VBA editor. This helps you see exactly what each line does.
- Keep your Personal Macro Workbook handy: If you have macros you use often, store them in
PERSONAL.XLSBso they’re available in all workbooks. - Name macros clearly: Use descriptive names like
FormatReportinstead ofMacro1to avoid confusion.
Troubleshooting
Macro doesn’t appear in the list: The macro may be in a different workbook. Check the “Macros in” dropdown. If it’s in an add-in, ensure the add-in is loaded (File > Options > Add-ins).
“Macros are disabled” error: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros (not recommended for security) or Enable VBA macros with notification. Also check if the file is blocked – right-click the file, select Properties, and check “Unblock” if present.
Macro runs but does nothing: The macro may be designed to act on a specific range or sheet that isn’t active. Check the code for references like Sheet1 or Range("A1"). Also, the macro may have been recorded with relative references – make sure the active cell is where you expect.
Runtime error when running: This often happens because the macro references an object that doesn’t exist. For example, if it tries to delete a sheet that isn’t there. Open the VBA editor, press F8 to step through, and see which line causes the error. Fix the code or adjust the data.
Macro works on my PC but not on a colleague’s: Security settings differ. Ask them to enable macros or add the file to their Trusted Locations (File > Options > Trust Center > Trusted Locations).
Frequently Asked Questions
Can I run a macro in Excel for the web? No. Excel for the web does not support VBA macros. You must open the file in the desktop version of Excel.
How do I run a macro automatically when opening a workbook? You can use the Workbook_Open event in VBA. In the VBA editor, double-click ThisWorkbook in the Project Explorer, then paste code like Private Sub Workbook_Open() followed by your macro name. Save and reopen the file.
Can I run a macro from a button on a worksheet? Yes. On the Developer tab, click Insert, select a Button (Form Control), draw it on the sheet, and assign the macro. You can also use a shape or picture – right-click it, choose Assign Macro.
Why is my macro grayed out in the Macro dialog? This usually means the macro is stored in a workbook that is not open, or the macro is part of an add-in that isn’t loaded. Make sure the workbook is open and the add-in is enabled.
Can I run a macro from another workbook? Yes, as long as both workbooks are open. In the Macro dialog, select the source workbook from the “Macros in” dropdown. Alternatively, you can reference it directly in VBA with Application.Run "'WorkbookName.xlsm'!MacroName".
What if I don’t see the Developer tab? Right-click the Ribbon and choose Customize the Ribbon. In the right panel, check the Developer box and click OK. The Developer tab appears.
Related
- Data Entry Pay: Hourly Rates & Salaries in 2024
- COUNTIF Formula in Google Sheets: How to Use It
- Random Number 1-30: How to Generate One
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Go To Next Iteration: Skip Loop Code CorrectlyVBA has no built-in Continue For or Continue Do statement. To move to the next iteration, either put…
- Excel VBA: Hide a Range of Cells or RowsIn Excel VBA, you can hide the entire rows or columns that contain a range, but you cannot…
- PowerPoint VBA: How to Loop Through SlidesTo loop through every slide in the open PowerPoint presentation, use a VBA For Each loop over ActivePresentation.Slides.…
- VBA For Loop Negative: How Step -1 WorksYes. A VBA For loop can count downward by using a negative Step, such as For i =…
- Excel VBA AutoFilter: How to Apply Filters with CodeIn Excel VBA, apply AutoFilter to a range that includes its header row, then set the field number…
- Excel Macro to Email Files From a Folder AutomaticallyIn Excel for Microsoft 365 on Windows, a VBA macro can let you choose a folder, attach its…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.