Curious Minds

Clear answers to science and history questions

Setting Up a Macro in Excel: Record, Edit, and Run It

· 6 min read · Science

To set up a macro in Excel for Microsoft 365 on Windows, enable the Developer tab, choose Developer > Record Macro, perform the actions you want to automate, and select Stop Recording. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm); then you can run the macro from the Developer tab, a keyboard shortcut, or a button.

What an Excel macro does

A macro is a recorded or programmed sequence of instructions that Excel can repeat. It is useful for predictable, repetitive work: applying the same formatting to weekly reports, cleaning imported data, or preparing a standard print layout. Excel’s recorder translates many actions into Visual Basic for Applications (VBA) code, the programming language used for traditional Office macros.

Recording is a practical starting point, but it does not make every action intelligent. A recorded macro generally repeats the specific steps and references captured during recording. If it was recorded while a particular cell was selected, it may return to that cell or act on a similarly fixed range when run later. For workbooks that change size or structure, the code may need editing so it can find the right data dynamically.

Show the Developer tab

The Developer tab is where Excel provides the macro recorder, the VBA editor, and controls for running macros. It may be hidden by default. In Excel for Microsoft 365 on Windows, show it as follows:

  1. Choose File > Options.
  2. Select Customize Ribbon.
  3. Under Main Tabs, select the Developer check box, then choose OK.

On Excel for Mac, open Excel > Preferences (or Settings, depending on the release), choose Ribbon & Toolbar, and enable Developer in the main tabs list. Excel for the web can open and edit a macro-enabled workbook, but it cannot create, run, or edit VBA macros. Use the desktop app for those tasks.

Record a macro step by step

Before recording, decide exactly what the macro should do and where it should operate. If the steps rely on a specific worksheet, select it before recording. Avoid unnecessary clicks and selections: the recorder may capture them, making the result harder to reuse.

  1. Choose Developer > Record Macro.
  2. Enter a short name with no spaces, such as FormatReport. Macro names can include letters, numbers, and underscores, but cannot begin with a number.
  3. Optionally enter a shortcut key and a description. Avoid replacing a familiar Excel shortcut: a macro shortcut can override the corresponding built-in shortcut while the workbook is open.
  4. Choose where to store the macro. This Workbook keeps it with the current file; Personal Macro Workbook makes it available in Excel on that computer. The latter is a hidden workbook that opens when Excel starts and is not automatically available on other computers.
  5. Choose OK, perform the intended actions, and then select Developer > Stop Recording.

For example, a report-formatting macro might select a title row, apply bold formatting and a fill color, set column widths, and format a date column. Test it on a copy or a small sample before using it on important data. The recorder may capture a particular cell address rather than the concept of “the current report’s title row.”

Save the workbook and choose a way to run it

A workbook saved as .xlsx cannot store VBA macro code. Use File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) to preserve the macro. If Excel warns that some features cannot be saved in a macro-free workbook, choose the option to keep the VBA project or cancel and select .xlsm instead.

To run a macro, choose Developer > Macros, select its name, and choose Run. Other convenient options include:

  • Keyboard shortcut: assign one in the Record Macro dialog or through the Macros dialog’s Options button. On Windows, Ctrl is used for the shortcut; on Mac, use the shortcut options shown in that version of Excel.
  • Worksheet button: choose Developer > Insert, select a button under Form Controls, draw it on the sheet, and assign the macro when prompted. Right-click the button later to edit its caption or change its assignment.
  • Quick Access Toolbar: use Excel’s customization options to add a macro command for convenient access.

Buttons and shortcuts make a macro easier to launch, but they do not change what it does. A button assigned to a destructive macro can still delete or overwrite data.

Edit and improve a recorded macro

Choose Developer > Macros, select a macro, and choose Edit to open the Visual Basic Editor. The code may look unfamiliar at first; recorded procedures commonly contain many selection and activation commands. Learning to recognize references such as worksheet names and cell ranges helps explain why a macro behaves differently when the workbook changes.

One important distinction is between selecting a range and operating on a known range. A recorder might produce code that selects a cell before formatting it. That can work in the original situation, but unnecessary selections make procedures more fragile. Editing VBA can make the macro more flexible, but changes should be made cautiously: a wrong reference or loop can affect many cells. Keep an untouched copy, change one thing at a time, and test on sample data.

Security, troubleshooting, and alternatives

Macros can make changes that are difficult to reverse, and VBA code can be used maliciously. Open macro-enabled workbooks only when you trust their source. Excel may block macros in files from the internet or show a security warning; do not enable content simply to make an unfamiliar file work. Follow your organization’s security policy rather than changing Trust Center settings to bypass protection. When a trusted file is blocked, ask the sender or administrator for an approved copy or signing process.

Macros cannot be undone with Ctrl+Z after they run. Save a separate copy before running unfamiliar or consequential code. If a macro will not run, check that the workbook is .xlsm, that the correct macro is selected, and that your organization has not blocked VBA. Also check for broken worksheet names, missing referenced files, or a macro that depends on a different active sheet or cell selection.

For simpler automation, consider whether a macro is necessary. Excel formulas recalculate results without running a procedure; tables, conditional formatting, and built-in sorting can handle many recurring tasks. Power Query is often a better fit for repeatable data import and transformation, while Office Scripts can automate some tasks in Excel for the web where supported. These tools have different capabilities and availability, so choose based on the task, platform, and workplace policies.

Frequently Asked Questions

Do I need to know VBA to create a macro?
No. Excel’s recorder can capture many basic actions without writing code. VBA knowledge becomes helpful when you need to adjust fixed cell references, add conditions, handle changing data, or troubleshoot unexpected behavior.

Why did my macro disappear after I saved?
The workbook may have been saved as .xlsx, a format that does not retain VBA code. Save it as .xlsm. If you chose the Personal Macro Workbook, the macro is stored there rather than in the workbook you were editing.

Can I use the same macro on another computer?
Yes, if the macro is stored in an .xlsm workbook and the other computer allows VBA macros. A macro in the Personal Macro Workbook is local to that Excel installation unless you separately transfer or recreate it.

Can I run a VBA macro in Excel for the web?
No. Excel for the web does not create, run, or edit VBA macros. Open the file in the Excel desktop application to use its VBA macros; Office Scripts may be an alternative for suitable web-based workflows.

Can I undo a macro with Ctrl+Z?
Usually not. Running VBA clears Excel’s normal undo history, so a macro’s changes generally cannot be reversed with Ctrl+Z. Save a copy first, especially if the procedure edits, deletes, or replaces data.

In this article
  1. What an Excel macro does
  2. Show the Developer tab
  3. Record a macro step by step
  4. Save the workbook and choose a way to run it
  5. Edit and improve a recorded macro
  6. Security, troubleshooting, and alternatives
  7. Frequently Asked Questions