How to Add a Macro in Excel: A Complete Guide

Macros are one of Excel's most powerful — and most underused — features. If you find yourself repeating the same sequence of steps over and over in a spreadsheet, a macro can collapse all of that into a single click. Here's how they work, how to add one, and what determines whether your approach will suit your situation.

What Is a Macro in Excel?

A macro is a recorded set of actions or a written script that Excel can replay on demand. Instead of manually formatting a report, sorting data, or running calculations every week, you record or write the macro once, then run it whenever you need it.

Macros in Excel are built on VBA (Visual Basic for Applications) — a programming language embedded in Microsoft Office. You don't need to know VBA to create basic macros, but understanding it opens up far more powerful possibilities.

Two Ways to Add a Macro in Excel

1. Record a Macro (No Coding Required)

The macro recorder is the fastest entry point. Excel watches what you do and converts your actions into VBA code automatically.

How to record a macro:

  1. Go to the View tab on the ribbon
  2. Click MacrosRecord Macro
  3. Give your macro a name (no spaces — use underscores, e.g., Format_Report)
  4. Optionally assign a shortcut key (e.g., Ctrl+Shift+F)
  5. Choose where to store it (more on this below)
  6. Click OK, then perform your actions in Excel
  7. When finished, go back to ViewMacrosStop Recording

Your macro is now saved and ready to run.

💡 If you don't see the Developer tab, enable it via File → Options → Customize Ribbon and check the Developer box. This tab gives you direct access to macro tools.

2. Write a Macro in the VBA Editor

For anything beyond simple recorded steps — conditional logic, loops, dynamic ranges, error handling — you'll write VBA directly.

How to open the VBA editor:

  1. Press Alt + F11 (Windows) or Option + F11 (Mac)
  2. In the Project pane, right-click your workbook and select Insert → Module
  3. Type or paste your VBA code in the module window
  4. Close the editor and run the macro via View → Macros → View Macros

A basic VBA macro looks like this:

Sub HighlightBlanks() Selection.SpecialCells(xlCellTypeBlanks).Interior.Color = RGB(255, 255, 0) End Sub 

This finds every blank cell in your selection and highlights it yellow — something the recorder could capture, but writing it directly gives you more control over scope and conditions.

Where to Store Your Macro

Where you save a macro determines who can use it and when. 🗂️

Storage LocationWhat It Means
This WorkbookMacro runs only in the current file
New WorkbookSaves to a new, separate workbook
Personal Macro WorkbookAvailable across all Excel files on your machine

The Personal Macro Workbook (PERSONAL.XLSB) is ideal for utility macros you want available everywhere. It loads silently in the background each time Excel opens.

If you're sharing a workbook with colleagues who need the macro too, store it in the workbook itself — and save the file as .xlsm (macro-enabled format). Standard .xlsx files strip out macros on save.

Running a Macro

Once added, you have several ways to trigger a macro:

  • Keyboard shortcut — assigned at the time of recording
  • View → Macros → Run — manual selection from the macro list
  • Developer tab → Macros — same as above with more options
  • Button on the sheet — insert a shape or Form Control button and assign the macro to it
  • Quick Access Toolbar — add the macro as a persistent toolbar button

For macros used frequently by non-technical users, a worksheet button is usually the most approachable option.

Security Settings Matter

Excel blocks macros by default in many configurations because VBA can be used maliciously. You may see a yellow security bar when opening an .xlsm file asking you to enable content.

Under File → Options → Trust Center → Trust Center Settings → Macro Settings, you can control how Excel handles macros:

  • Disable all macros with notification — the default; safest for most users
  • Disable all macros without notification — strictest; macros never run
  • Enable all macros — not recommended for general use
  • Disable all except digitally signed macros — useful in corporate environments

If you're working in an organization with IT policies, macro access may be controlled at an administrative level regardless of your personal settings.

Variables That Shape Your Approach

How you add and use macros in practice depends on several factors:

Excel version and platform. The macro recorder and VBA editor are available in Excel for Windows and Mac, but the Mac version has some VBA limitations — particularly around system-level interactions like file paths and certain API calls. Excel Online (the browser version) does not support VBA macros at all, though Microsoft is developing Office Scripts as a web-based alternative.

Technical comfort level. Recorded macros are genuinely accessible to beginners. But the recorder has blind spots — it captures literal cell references instead of dynamic ranges, and it can't handle decision-making. Users who want macros that adapt to changing data will hit a ceiling without some VBA knowledge.

Workbook sharing and collaboration. If your file lives in SharePoint or OneDrive and is opened by multiple users simultaneously, macro behavior can become unpredictable. Macro-enabled files also sometimes face restrictions in shared or managed environments.

Frequency and complexity of the task. A macro that formats a static monthly report is straightforward to record and maintain. A macro that pulls data from external sources, applies conditional logic, and emails a summary is a different scope entirely — and whether that's the right tool depends on the scale of the workflow and what other tools are already in use. 🔧

The gap between "I recorded a working macro" and "I have a reliable, maintainable macro for a real workflow" varies significantly depending on the complexity of what you're automating and how your Excel environment is configured.