What Is a Macro in Excel? Beginner's Guide to Excel Macros – LicenceHouse guide

A macro in Excel is a saved set of actions that Excel can replay with one click or shortcut. Behind the scenes it is a short program written in VBA (Visual Basic for Applications). To make one, go to Developer > Record Macro, carry out the steps, then select Stop Recording. Run it again from Developer > Macros (Alt+F8).

  1. Show the Developer tab: File > Options > Customize Ribbon, tick Developer, select OK.
  2. Select Developer > Record Macro, name the macro and choose where to store it.
  3. Perform the actions you want to automate.
  4. Select Developer > Stop Recording.
  5. Save the file as an Excel Macro-Enabled Workbook (.xlsm).
  6. Run the macro with Alt+F8, choose it, and select Run.

What are macros in Excel used for?

Macros automate repetitive work. If you format the same weekly report, clean the same export or copy data between the same sheets every day, a macro can do it in seconds and the same way every time. Common examples are applying a house style to a header row, deleting unwanted columns from a system export, and refreshing and printing a set of sheets.

A recorded macro stores each click as a line of VBA code. You don't need to know any code to record and run one, but learning a little VBA lets you modify macros and add logic that recording can't capture, such as decisions and loops.

How do I make an Excel macro?

Record it. The Developer tab is hidden by default, so turn it on first. Microsoft's guide to showing the Developer tab covers this, and its quick start for creating a macro shows the recording steps.

Steps to record an Excel macro: show the Developer tab, Record Macro, name it FormatHeader, perform the steps, stop, save as .xlsm.

  1. Go to File > Options > Customize Ribbon, tick Developer in the right-hand list and select OK. On a Mac, go to Excel > Settings > Ribbon & Toolbar and tick Developer.
  2. Select Developer > Record Macro. You can also use View > Macros > Record Macro.
  3. Enter a name with no spaces, such as FormatHeader.
  4. Optionally set a shortcut key. Use an uppercase letter, which makes it Ctrl+Shift+letter, so you don't overwrite a built-in shortcut such as Ctrl+C.
  5. Under Store macro in, choose This Workbook to keep it with the file, or Personal Macro Workbook to make it available in every workbook on your PC.
  6. Select OK, perform your steps, then select Developer > Stop Recording.
  7. Save with File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). A normal .xlsx file cannot hold macros, and Excel will strip them if you save in that format.

Advanced nuance: by default the recorder uses absolute references, so a macro recorded on A1:E1 always formats A1:E1. Select Developer > Use Relative References before recording if you want the macro to act relative to whichever cell is active when you run it, for example "format the row I'm on".

How do I run an Excel macro?

Press Alt+F8 (or select Developer > Macros), choose the macro in the list and select Run. If you assigned a shortcut key while recording, press that instead. On a Mac, use Developer > Macros.

For macros other people will use, a button is friendlier:

  1. Select Developer > Insert and, under Form Controls, choose Button.
  2. Draw the button on the sheet. The Assign Macro dialog opens.
  3. Pick the macro and select OK. Right-click the button and choose Edit Text to rename it.

You can also add a macro to the Quick Access Toolbar: File > Options > Quick Access Toolbar, choose Macros under "Choose commands from", add it and select OK.

How do I modify an Excel macro?

Press Alt+F8, select the macro and choose Edit. Excel opens the Visual Basic Editor (you can also press Alt+F11) with the macro's code in a module. Change the code, then close the editor; changes save with the workbook.

Here is a cleaned-up version of a recorded header-formatting macro:

Sub FormatHeader()
    With Range("A1:E1")
        .Font.Bold = True
        .Interior.Color = RGB(221, 235, 247)
    End With
    Columns("A:E").AutoFit
End Sub

Recorded code is usually longer than it needs to be because it captures every scroll and selection. Deleting lines such as ActiveWindow.SmallScroll and replacing Range("A1").Select followed by Selection.Font.Bold = True with Range("A1").Font.Bold = True makes it faster and easier to read.

What we commonly see: a macro runs perfectly once and then fails next week with "Subscript out of range". The cause is almost always a hard-coded sheet or file name, such as Sheets("Report Nov"), that no longer exists. Refer to ActiveSheet or ask for the name with an InputBox instead.

How do I write an IF statement in an Excel macro?

In VBA, use If ... Then ... ElseIf ... Else ... End If. Unlike the worksheet IF function, it runs actions rather than returning a value. This macro checks stock levels in column D and writes a status in column E:

Table of FlagStock VBA macro results: stock 120 gives OK, stock 0 gives Reorder and stock 35 gives Low in column E.

Sub FlagStock()
    Dim r As Long
    For r = 2 To Cells(Rows.Count, "D").End(xlUp).Row
        If Cells(r, "D").Value = 0 Then
            Cells(r, "E").Value = "Reorder"
        ElseIf Cells(r, "D").Value < 50 Then
            Cells(r, "E").Value = "Low"
        Else
            Cells(r, "E").Value = "OK"
        End If
    Next r
End Sub

With this data, the macro fills column E as shown:

Row D (Stock) E (Status after macro)
2 120 OK
3 0 Reorder
4 35 Low

Combine conditions with And and Or, for example If Cells(r, "D").Value < 50 And Cells(r, "C").Value = "Core" Then. If you only need a result in a cell, a worksheet IF formula is simpler than a macro.

Why are macros disabled or blocked in Excel?

Macros can run harmful code, so Excel disables them in files you haven't trusted. When you open a macro workbook, a yellow message bar appears with Enable Content; select it only if you trust the source. The settings live under File > Options > Trust Center > Trust Center Settings > Macro Settings, as Microsoft explains in enable or disable macros in Microsoft 365 files.

Table of blocked macro messages: yellow Enable Content bar, red Security Risk bar fixed with Unblock, and greyed-out tools from inactive Office.

Files downloaded from the internet or received by email are treated more strictly on Windows. Excel shows a red Security Risk bar with no Enable button. If you trust the file, close it, right-click it in File Explorer, choose Properties, tick Unblock on the General tab and select OK. Microsoft's article on macros from the internet being blocked explains why.

If the Record Macro button and most other commands are greyed out, Excel may be running in reduced functionality mode because Office isn't activated. Our guide to fixing an unlicensed Office product covers that.

Which versions of Excel support macros?

VBA macros work in every desktop version of Excel: 2016, 2019, 2021, 2024 and Microsoft 365, on both Windows and Mac. Excel for the web, iPad and phones can open .xlsm files but cannot record or run VBA macros. Some Windows-only VBA features, such as certain ActiveX controls, don't work on a Mac.

Microsoft 365 business plans also offer Office Scripts on the Automate tab, which use TypeScript instead of VBA and run in Excel for the web, Windows and Mac. Our Excel 2024 vs Microsoft 365 comparison covers which automation and AI features each edition gets. If you need a perpetual licence that runs VBA on Windows, Office 2024 Professional Plus for Windows includes the full Developer tab and Visual Basic Editor.

Frequently asked questions

Do I need to know VBA to use macros?

No. The macro recorder writes the VBA for you while you click through a task, and you can run the result without ever opening the code. Learning basic VBA becomes useful when you want to edit a recorded macro, remove unnecessary steps, or add decisions and loops, such as an If statement that treats each row differently.

Are Excel macros safe?

Macros you record yourself are safe. Macros in files from unknown sources can be dangerous, because VBA can delete files or download malware. That is why Excel disables macros by default and blocks those in internet downloads. Only select Enable Content or Unblock when you know who made the file and expected it to contain macros.

Where are Excel macros stored?

A macro is stored either in the workbook where you recorded it, which must be saved as .xlsm, or in the Personal Macro Workbook, a hidden file named PERSONAL.XLSB that opens with Excel and makes its macros available in every workbook. You can see both in the Visual Basic Editor's Project pane, opened with Alt+F11.

Can I undo a macro in Excel?

No. Running a macro clears Excel's undo history, so Ctrl+Z cannot reverse what it did. Save the workbook before testing a new macro, or work on a copy. For macros that change a lot of data, add a line that copies the sheet first, so you always have a backup to compare against.

How do I delete a macro in Excel?

Press Alt+F8, select the macro and choose Delete. To remove every macro, save the workbook as an ordinary Excel Workbook (.xlsx); Excel warns that the VBA project will be lost. Macros in the Personal Macro Workbook can only be deleted after you unhide it with View > Unhide.

Related guides

Links in this guide

Link Type Why it helps
Microsoft Support: Show the Developer tab External Official steps to reveal the macro tools
Microsoft Support: Quick start, create a macro External Official recording walkthrough
Microsoft Support: Enable or disable macros External Explains each Trust Center macro setting
Microsoft Learn: Macros from the internet are blocked External Why downloaded macro files are blocked and how to unblock them
Fix "Unlicensed Product" in Office Internal Fixes greyed-out commands caused by activation
Excel 2024 vs Excel in Microsoft 365 Internal Automation features by edition
Office 2024 Professional Plus for Windows Internal Perpetual Excel with full VBA support
Excel keyboard shortcuts Internal Shortcuts you can automate or pair with macros
What is VLOOKUP in Excel? Internal A formula alternative to many lookup macros
Excel tutorial for beginners Internal Basics to learn before automating
Office 2024 Home & Business vs Professional Plus Internal Compares editions that include Excel
Excel, Excel tips, How-to, Vba