
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.
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:
- Right-click anywhere on the ribbon and select “Customize the Ribbon…”.

- In the Excel Options dialog, in the right-hand panel, check the “Developer” checkbox.

- Click “OK”.
- You’ll find the Developer tab next to the View tab.
If you’re on a Mac, follow these steps:
- Click “Excel” (top left).
- Click “Preferences” (called “Settings” in newer versions of macOS).
- Select “Ribbon & Toolbar”.
- In the dialog, check the “Developer” box in the right-hand panel.
- Click “Save”.
- 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:
- 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.
- 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.
- Select “Record Macro” on the Developer tab.

- 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.
- 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.

- 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.
- Optionally, you can add a description. It’s useful when more than one person works with these macros.

- Click “OK”.
- 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).

- Select “Stop Recording”.

- Done! You just created your first macro!
Now, to run the macro:
- Select the cells you want to apply the macro to and click “Macros” on the Developer tab.

Ninja Tip: you can press Alt+F8 instead of clicking “Macros”.
- Select the macro you want to run and click “Run”.

- That’s it! You can already see the results.

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
- Select Visual Basic on the Developer tab.

- 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.

- Now let’s start writing the macro. Type the keyword “Sub” followed by the macro’s name, in our case MyFirstMacro.


- 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.

- 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".

- 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!

- Now let’s run our macro on the students’ attendance data. Select the attendance cells and click Macros.

- A dialog box with your macros opens. Select the one you want to run, in our case MyFirstMacro, and click Run.

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:
- 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.
- 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
- Click “Record Macro”, name it, and save it in the right workbook. We’ll call ours “AbsoluteMacroExample”.


- Click B3, type in the column headers, and click “Stop Recording”.

- Now, when you come across another table of the same kind, in a different workbook or worksheet, click “Macros”.

- Choose the macro you created and click “Run”.

- 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.

Recording macros in relative mode
- Click “Use Relative References”.

- Select the cell you want to fill in (B3), then click “Record Macro”. Name it and save it. We’ll call ours “RelativeMacroExample”.

- Type in the column headers, and click “Stop Recording”.

- Now, when you come across another table of the same kind, click “Macros”.

- Choose the macro you created and click “Run”.

- 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.

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.

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

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

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.”

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.
- Create a macro. In this case, we’ll create one that enters today’s date in cell C1. To do that, click “Record Macro”.

- 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.
- In cell C1, type
=TODAY()and click “Stop Recording”.


- 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.

- Under “Choose commands from:”, select “Macros”.

- Select the macro you want on the toolbar. In our example, it’s Macro17, saved in the Personal Macro Workbook. Click “Add”.

- You can press “Modify” to choose an icon for that macro, then click “OK”.

- Then click “OK” again.

- Now you can see the macro’s icon on the toolbar at the top. Clicking the icon runs the macro.

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:

Macro to print a sheet
To print the current Excel sheet, write the following code:

Macro to save a file
To save a file, write the following code:

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.

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.

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.
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.

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.

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.



