📊 Learn Excel inside Excel. Try Ninja Excel free for 15 days, no credit card required. Start free trial

How to Create Macros in Excel

Man reviewing a marketing budget spreadsheet in Excel on a desktop monitor

Quick answer

A macro in Excel is a saved set of actions you can run again with a single click, instead of repeating the same clicks and keystrokes by hand. You record one with the Developer tab’s Record Macro button, which needs no coding, or you write it directly in the Visual Basic Editor (VBE) using VBA. Save any workbook that contains macros as .xlsm, the macro-enabled Excel format, or the code will be lost.

Summarize this with your favorite AI

Opens a new conversation with this article ready to analyze.

A macro is an action, or a series of actions, that you can run as many times as you want, because the instructions stay saved inside your Excel workbook until you need them. With macros you can automate functions, forms, tables and more complex tasks. Think of a macro as a regular Excel feature that you program yourself to do exactly what you want. This guide walks through recording your first macro, writing one directly in VBA, the difference between absolute and relative references, a common mistake that trips up beginners, and how to pin a macro to your Quick Access Toolbar.

What you’ll learn in this article

  • How to enable the Developer tab and record your first macro
    turn on the hidden tab, record a macro with the Macro Recorder, and run it on a real worksheet.
  • How to write the same macro in the Visual Basic Editor (VBE)
    open the editor with Alt+F11, create a Sub procedure, and understand what each line of VBA does.
  • Absolute references versus relative references
    why the same macro can fill in the wrong cells depending on which mode it was recorded in, with a side-by-side example.
  • The most common mistake with relative references
    why dragging a formula while recording only fills in a fixed number of rows, not the whole table.
  • How to pin a macro to the Quick Access Toolbar
    a full walkthrough, with screenshots, so your macro is one click away every time you open Excel.

By the end of this article: you’ll be able to record, run, and troubleshoot your own macros, and decide when it’s worth switching from the Macro Recorder to VBA.

Reading time: 14 minutes | Level: beginner to intermediate | For: anyone who repeats the same clicks in Excel every day

What is a macro in Excel?

A macro is an action, or series of actions, that you can run as many times as you want, because the instructions stay saved inside an Excel workbook until you need them. With macros you can create and modify functions, forms, tables, and more complex tools. It works like any other Excel feature, except you program it to do exactly what you want.

What are macros in Excel used for?

Purpose: A macro lets you automate repetitive Excel tasks. With a single click, every task you programmed runs at once.

Advantages: you avoid mistakes, save time, and keep your work more organized.

How to enable macros in Excel

Before you can record a macro, you need to make the Developer tab visible in Excel. It’s hidden by default.

If you’re on Windows, follow these steps:

  1. Right-click anywhere on the ribbon and select “Customize the Ribbon…”.
    Excel ribbon right-click menu showing the Customize the Ribbon option
  2. In the Excel Options dialog, in the right-hand panel, check the “Developer” checkbox.
    Excel Options dialog with the Developer tab checked under Customize Ribbon
  3. Click “OK”.
  4. You’ll find the Developer tab next to the View tab.

If you’re on a Mac, follow these steps:

  1. Click “Excel” (top left).
  2. Click “Preferences” (called “Settings” in newer versions of macOS).
  3. Select “Ribbon & Toolbar”.
  4. In the dialog, check the “Developer” box in the right-hand panel.
  5. Click “Save”.
  6. You’ll find the Developer tab next to the View tab.

How to create macros in Excel

There are two ways to create a macro in Excel:

  1. Macro Recorder: this tool is ideal if you don’t know how to code but still want to use macros. Excel records every click and keystroke you make so it can repeat the same instructions later. This article explains how to use it.
  2. Visual Basic for Applications (VBA): the language macros are written in. Excel’s Visual Basic Editor lets you create and edit macros directly in this language. If you know some programming or already know VBA, this can be the better option.

Ninja Tip: a common and very useful practice is to combine both: record a macro to quickly get a first working version, then tweak it in VBA for more flexibility and more options.

Ninja Tip: when you save a workbook that contains macros, save it as .xlsm, the Excel file format that supports macros.

Recording macros

Let’s start by recording a macro that changes a cell’s format to percentage.

Ninja Tip: macro names must start with a letter. Spaces and symbols aren’t allowed in the name.

  1. Select “Record Macro” on the Developer tab.
    Excel Developer tab with the Record Macro button highlighted
  2. To save your macro, you first need to name it. Choose a short name that describes what it does and is easy to recognize once you have several macros.
  3. Choose where to save the macro. Usually, you save it in the same workbook.

Ninja Tip: if you save the macro in this workbook and want to use it in another one, you’ll need to open this workbook first to access the macro. If you save it in the “Personal Macro Workbook” instead, the macro is available in every workbook you open.

Record Macro dialog box with the Store macro in dropdown open

  1. Optionally, you can create a keyboard shortcut. A shortcut is a key combination assigned to run the macro.

Ninja Tip: even though a shortcut is handy for an action you perform often, be careful it doesn’t clash with a shortcut Excel already uses. You can use Ctrl+Shift+letter to avoid that kind of conflict.

  1. Optionally, you can add a description. It’s useful when more than one person works with these macros.
    Record Macro dialog box with a macro description filled in
  2. Click “OK”.
  3. Now perform the actions you want to repeat. In this case, click Home and then Percent Style. Don’t select another cell, and don’t reselect A1, because that changes what the macro records (we cover this in more detail further down).
    Excel Home tab tooltip showing the Percent Style shortcut
  4. Select “Stop Recording”.
    Excel Developer tab with the Stop Recording button highlighted
  5. Done! You just created your first macro!

Now, to run the macro:

  1. Select the cells you want to apply the macro to and click “Macros” on the Developer tab.
    Excel Developer tab View Macros tooltip above a student attendance table

Ninja Tip: you can press Alt+F8 instead of clicking “Macros”.

  1. Select the macro you want to run and click “Run”.
    Macro dialog box listing MyFirstMacro with the Run button
  2. That’s it! You can already see the results.
    Excel worksheet showing attendance values formatted as percentages

Ninja Summary

  • A macro saves the clicks and keystrokes you perform so you can play them back with one click.
  • You can create one two ways: recording it with the Macro Recorder, or writing it in VBA.
  • Save any workbook with macros as .xlsm, or the code won’t be kept.

Writing macros in Excel with the Visual Basic Editor

  1. Select Visual Basic on the Developer tab.
    Excel ribbon with the Visual Basic button highlighted on the Developer tab
  2. The Visual Basic Editor (VBE) opens, and we need to create our first macro. First, double-click the sheet you’ll work on in the Project Explorer to open its code window.
    Visual Basic Editor window with the Project Explorer and an empty code window
  3. Now let’s start writing the macro. Type the keyword “Sub” followed by the macro’s name, in our case MyFirstMacro.
    Empty VBA code window titled Book1 - Sheet1 (Code)
    VBA code window with Sub MyFirstMacro typed on the first line
  4. Press Enter, and the editor automatically adds the parentheses after the name and an “End Sub” line, which marks where the subroutine, meaning the macro, ends.
    VBA code window showing Sub MyFirstMacro with End Sub auto-completed
  5. Now, between Sub and End Sub, write the macro’s code. Since we want to apply a percentage format to the cells, write the code Selection.Style = "Percent".
    VBA code window with the Selection.Style equals Percent line added
  6. Switch back to Excel by clicking the View Microsoft Excel button (the Excel icon at the left end of the toolbar), and that’s it: you just wrote your first macro!

Visual Basic Editor toolbar with the Excel icon highlighted to switch back to the workbook

  1. Now let’s run our macro on the students’ attendance data. Select the attendance cells and click Macros.
    Small Excel ribbon with the Macros button highlighted above a worksheet
  2. A dialog box with your macros opens. Select the one you want to run, in our case MyFirstMacro, and click Run.
    Macro dialog box with the Run button highlighted

Done! Now the attendance values are shown as percentages, exactly like when we recorded the same macro earlier.

Using relative references in macros

There are two types of references in macros:

  1. Absolute reference: the cells you select while recording the macro stay fixed. In other words, every time you run the macro, it fills in the exact same cells, no matter which cell was selected when you ran it. Macros record in absolute mode by default.
  2. Relative reference: the macro runs on cells relative to whichever cell was selected when you ran it.

Ninja Tip: relative references are usually the better choice, since they give the macro more flexibility when you run it.

For example, say that every week you export an Excel table that needs the same column headers typed in. You could record a macro that fills in those headers for you. Let’s see what happens when we record it in absolute mode versus relative mode.

Recording macros in absolute mode

  1. Click “Record Macro”, name it, and save it in the right workbook. We’ll call ours “AbsoluteMacroExample”.
    Excel Developer tab Record Macro tooltip above a table missing its headers
    Record Macro dialog box named AbsoluteMacroExample
  2. Click B3, type in the column headers, and click “Stop Recording”.
    Excel Developer tab Stop Recording tooltip above a filled contact table
  3. Now, when you come across another table of the same kind, in a different workbook or worksheet, click “Macros”.
    Excel Developer tab Macros tooltip above a new unlabeled table
  4. Choose the macro you created and click “Run”.
    Macro dialog box with AbsoluteMacroExample selected
  5. Notice it writes the headers in the exact same place as when we created the macro. That’s what “absolute” means: it follows the instructions to the letter, writing “First name” in B3, “Last name” in C3, and so on, regardless of which cell was selected when you ran it. Here the macro misses its target, because this table actually starts further down and to the right, not at B3.
    Excel worksheet showing an absolute reference macro writing headers in the wrong place

Recording macros in relative mode

  1. Click “Use Relative References”.
    Excel Developer tab with Use Relative References highlighted above a table
  2. Select the cell you want to fill in (B3), then click “Record Macro”. Name it and save it. We’ll call ours “RelativeMacroExample”.
    Excel Developer tab Record Macro tooltip with cell B3 selected
  3. Type in the column headers, and click “Stop Recording”.
    Excel Developer tab Stop Recording tooltip above a completed table
  4. Now, when you come across another table of the same kind, click “Macros”.
    Excel Developer tab Macros tooltip above a new table that still needs its headers
  5. Choose the macro you created and click “Run”.
    Macro dialog box with RelativeMacroExample selected
  6. Now the instructions run based on whichever cell was selected when you started the macro. When we created the macro, we started at B3 and typed into B3, C3, D3 and E3. So now, selecting C4 and running the macro types into C4, D4, E4 and F4 instead. The macro remembers that it needs to type “First name” into the cell you started on, and then “Last name” into the cell right next to it.
    Excel worksheet showing a relative reference macro filling headers correctly

Ninja Summary

  • Absolute references always write to the exact same cells, every time the macro runs.
  • Relative references write based on whichever cell is selected when you run the macro.
  • Turn on Use Relative References before you click Record Macro, not after.

A common mistake with relative references

A common mistake when using macros in Excel is dragging a formula to fill in a table and assuming relative mode will fill in the whole table for you. For example, say you created a macro that adds a new column with a formula. A common mistake is dragging that formula all the way to the bottom of the table while recording, then assuming the macro will work the same way on a table with a different number of rows.

In this example, we used relative references to record the macro and fill in the table.

Excel Developer tab Record Macro tooltip above a length, width and area table

Then, we entered the formula in D4 and dragged it down to fill the table.

Excel worksheet with an area formula dragged down to fill a table

Now let’s see what happens when we run this macro on a different table:

Excel Developer tab Macros tooltip above a lot number table with an empty area column

Notice that running the macro doesn’t fill in every cell of the table. It only fills E4 through E7. That’s because the recording captured the instruction as “fill in this cell and the three below it,” not “fill in however many rows this table has.”

Excel worksheet showing a macro that only filled four of seven area cells

Adding a macro to the Quick Access Toolbar

Adding a macro to the toolbar at the top of the screen is very useful for actions you need to perform often.

  1. Create a macro. In this case, we’ll create one that enters today’s date in cell C1. To do that, click “Record Macro”.
    Excel Developer tab with the Record Macro tooltip showing
  2. Name it and choose which workbook to save it in.

Ninja Tip: no matter where you choose to save the macro, its icon gets installed and shows up in every Excel workbook you open. If you save it in Book1 and want to run it from Book2, you’ll still need to open Book1 first. If you’d rather not open Book1 every time, save the macro in the “Personal Macro Workbook” instead.

  1. In cell C1, type =TODAY() and click “Stop Recording”.
    Excel worksheet with the TODAY formula entered in cell C1
    Excel Developer tab with the Stop Recording tooltip showing
  2. Once the macro is created, click the small arrow at the end of the Quick Access Toolbar, in the green bar at the top of the window. A menu opens. Select “More Commands…” from that menu.
    Excel Quick Access Toolbar menu showing the More Commands option
  3. Under “Choose commands from:”, select “Macros”.
    Excel Options dialog with Macros selected under Choose commands from
  4. Select the macro you want on the toolbar. In our example, it’s Macro17, saved in the Personal Macro Workbook. Click “Add”.
    Excel Options dialog showing a macro added to the Quick Access Toolbar
  5. You can press “Modify” to choose an icon for that macro, then click “OK”.
    Excel Options Modify Button dialog for choosing a macro icon
  6. Then click “OK” again.
    Excel Options dialog with the Modify button highlighted after changing the macro icon
  7. Now you can see the macro’s icon on the toolbar at the top. Clicking the icon runs the macro.
    Excel Quick Access Toolbar showing a new macro icon above a worksheet

Ninja Tip

The Quick Access Toolbar isn’t tied to one workbook: once you pin a macro there, its icon shows up no matter which file you open, as long as the workbook that holds the macro’s code (or the Personal Macro Workbook) is also open.

Examples of macros in Excel

Macro to copy and paste cells

To copy cells within the same sheet in Excel, write the following code:

VBA code for copying and pasting a single cell and a range of cells

Macro to print a sheet

To print the current Excel sheet, write the following code:

VBA code for printing the active worksheet

Macro to save a file

To save a file, write the following code:

VBA code for saving and closing the active workbook

Macro to format a range of cells

To change the format of a range of cells, use the following code and adjust it to your needs.

VBA code for changing the number format and font of a cell range

First, select the range of cells with the code Range().Select, then edit it however you need.

Macro to look up data with Excel VBA

To look up a value in a data set, write the following code. The macro returns the address of the first cell that matches the value you’re looking for.

VBA code for finding a value in a range with MsgBox

First, you specify the range of cells to search, then the code determines which cell holds the value you’re looking for.

Conclusion

An Excel macro is an action, or a set of actions, that you can run as many times as you need. It’s a very useful tool because it automates repetitive tasks, and it also cuts down on mistakes and the time a task takes.

Put macros to work in your own spreadsheets

Reading about macros only gets you so far. The fastest way to make them stick is to record and run a few inside a real workbook.

Still doing the same clicks every single week?

Macros save hours once you know when to use absolute references, relative references, or a quick line of VBA. Ninja Excel’s online courses walk you through live exercises inside Excel itself, with progress reports for your manager if your company is sponsoring the training.

See the Excel courses

You start with a level check, so you never repeat what you already know.

FAQ

How do I create a macro?

First you need to enable the “Developer” tab, which is hidden by default in Excel. Once it’s visible, you can record your macros from it.

How do I use a macro?

A macro is a saved set of actions you can run whenever you need them. That way, actions you’d normally repeat by hand can run without you doing them manually each time.

What are macros in Excel?

Macros are automated actions that you can repeat as many times as you need. Excel records your clicks and keystrokes so it can play them back later.

How do I create a form in Excel with macros?

On the Developer tab, select Visual Basic. In the Visual Basic Editor, open the Insert menu and click UserForm.

Visual Basic Editor Insert menu with UserForm highlighted

Done! Now you’ll see a blank form where you can add whatever controls you need.

How do I protect a sheet with macros?

To protect an Excel sheet using macros, write the following code in the Visual Basic Editor. Then run the macro to protect or unprotect the sheet.

VBA code for protecting and unprotecting a worksheet
How do I delete a macro in Excel?

On the Developer tab, click Macros. In the dialog box, select the macro you want to delete, click the Delete button, and you’re done.

Macro dialog box with the Delete button highlighted

Table of contents