Running a macro means executing a saved sequence of commands or actions in a program, most commonly in Microsoft Excel, to automate repetitive tasks. In Excel, you run a macro by pressing Alt+F8, selecting the macro name, and clicking Run—or by assigning the macro to a button or keyboard shortcut. Macros are written in VBA (Visual Basic for Applications), and they can save hours of manual work, but they also carry security risks, so you should only run macros from sources you trust.
What Is a Macro, Really?
A macro is a set of programming instructions that tells the software what to do step by step. In Excel, you can create a macro without writing any code by using the Record Macro feature: Excel watches your actions (like formatting cells, entering formulas, or sorting data) and converts them into VBA code. When you run that macro later, it replays those actions exactly. Macros can also be written manually in the VBA editor for more complex logic, such as looping through rows, making decisions, or interacting with other files.
How to Run a Macro in Excel (Step-by-Step)
Here’s the quickest way to run a macro in Excel for Microsoft 365 on Windows:
- Open the workbook that contains the macro.
- Press Alt+F8 to open the Macro dialog box.
- Select the macro you want to run from the list.
- Click Run.
If the macro isn’t in the list, it might be stored in a different workbook or in your Personal Macro Workbook. On a Mac, press Option+F8 instead. If you use Excel for the web, you cannot run macros—they only work in the desktop versions.
How to Choose Which Macro to Run
When you open the Macro dialog, you’ll see a list of available macros. To choose the right one, consider these factors:
- Name: Macros should have descriptive names like FormatReport or ClearInput. If the name isn’t clear, you may need to look at the code first.
- Location: The dialog shows the workbook where each macro is stored. Only run macros from the current workbook unless you know the source is safe.
- Description: Some macros include a description in the code comments. You can view it by selecting the macro and clicking Edit to open the VBA editor.
If you’re unsure what a macro does, don’t run it blindly. Open the VBA editor (Alt+F11) and read the code to verify it won’t delete data or make unwanted changes.
Common Problems When Running Macros
Even simple macros can fail. Here are typical issues and how to fix them:
- Macro disabled: Excel shows a security warning bar. Click Enable Content to allow the macro to run. If macros are disabled by policy, you may need to change Trust Center settings.
- “Cannot run the macro” error: This often means the macro is not available in the current workbook. Check that the workbook is open and the macro is stored there or in the Personal Macro Workbook.
- Runtime errors: A macro may stop with a message like “Run-time error ‘1004’.” This usually means the macro refers to a range or sheet that doesn’t exist. You can click Debug to see the line that caused the problem.
- Macro works on wrong data: If the macro was recorded with specific cells selected, it will always operate on those same cells. To make it flexible, you need to edit the VBA code to use relative references.
Macros vs. Other Automation Tools
Macros are not the only way to automate Excel. Here’s how they compare:
- Formulas: For simple calculations, formulas are safer and easier to understand. Macros are overkill for a single SUM or IF.
- Power Query: If you’re cleaning or transforming data from external sources, Power Query (Data > Get Data) is more robust and doesn’t require VBA. It’s also easier to refresh.
- Office Scripts: In Excel for the web, you can use Office Scripts (JavaScript-based) instead of VBA. They run in the browser, but they can’t do everything VBA can.
- VBA Macros: Best for complex, interactive tasks that need to manipulate the Excel interface, like creating custom forms or automating a multi-step report.
Costs and Security Considerations
Macros themselves are free—they’re a built-in feature of Excel. The real cost is the time to learn VBA and the risk of running malicious code. A macro can contain commands that delete files, send emails, or install malware. To stay safe:
- Only run macros from trusted sources. If you receive a workbook from an unknown sender, don’t enable macros.
- Keep macros in a workbook saved as .xlsm (macro-enabled). The .xlsx format cannot store macros.
- Before running a macro, make a backup copy of your workbook. Macros cannot be undone with Ctrl+Z, so if a macro corrupts your data, you’ll need the backup.
- Use digital signatures: if a macro is signed by a trusted publisher, Excel will show that. Unsigned macros are more suspicious.
Alternatives to Running a Macro
If you’re hesitant about macros, consider these alternatives:
- Keyboard shortcuts: For simple repetitive actions, learn shortcuts like Ctrl+Shift+L for filters or Ctrl+T for tables.
- Templates: Save a formatted workbook as a template (.xltx) so you don’t have to reapply formatting.
- Conditional formatting and data validation: Automate visual cues and input restrictions without code.
- Power Automate: For workflows that involve multiple apps, Power Automate can trigger actions based on events, but it doesn’t replace VBA inside Excel.
Frequently Asked Questions
Can I run a macro automatically when I open a workbook? Yes. You can name a macro Auto_Open or use the Workbook_Open event in VBA. However, Excel may disable macros on open, so the macro might not run unless the user enables content.
How do I assign a macro to a button? Right-click a shape or button, choose Assign Macro, and select the macro. Then clicking that button runs the macro. This is easier than remembering Alt+F8.
Why is my macro not working on another computer? The macro may rely on specific references (like ActiveX controls) that aren’t installed, or the other computer may have macros disabled. Also, the file path or sheet names may differ. Always test on a clean machine.
Can I undo a macro after running it? No, macros cannot be undone with Ctrl+Z. That’s why you should always save a copy of your workbook before running a macro. If you accidentally run a destructive macro, you’ll need to restore from the backup.
Is VBA the same as a macro? Not exactly. A macro is the recorded action or the code that performs it. VBA is the language used to write that code. You can write VBA code manually without recording a macro, and every recorded macro is VBA code.
Do macros work in Google Sheets? No, Google Sheets uses Apps Script (JavaScript), not VBA. You can convert some simple macros, but the code is different. In Excel for the web, macros are not supported at all.
Related
- Random Number 1-100: How to Generate One
- Highlight All Shortcut: Excel, Docs & More
- Data Entry Requirements: Skills, Tools, and Steps
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Date Picker: Controls, Options, and FixesExcel for Microsoft 365 on Windows does not include a universal VBA date-picker control that you can simply…
- Excel VBA UserForm Progress Bar: Build One That WorksTo add an Excel VBA UserForm progress bar, create a UserForm with a Frame and a colored Label…
- VBA VarType: What It Is and How to Use ItIn VBA, “var type” almost always refers to VarType, a built-in function that returns a number identifying the…
- Excel VBA ComboBox: Load a UserForm List From RangeTo fill an Excel VBA UserForm ComboBox from cells, set its RowSource property to a worksheet range, or…
- Excel UserForm Scrollbar: Setup, Events, and ExamplesTo use a scrollbar on an Excel UserForm, add a ScrollBar control in the Visual Basic Editor, set…
- Copy and Paste Values in VBA: Methods and ExamplesTo copy only cell values with Excel VBA, assign one range’s Value2 to another range of the same…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.