Excel macros: what they are, how to make one and how to run it

A macro takes over the clicks you repeat every week: tidying an export, deleting blank rows, setting up a sheet per team. This page gives you the big picture, from making and running a macro to turning macros on safely and knowing which versions of Excel can run them. You can write your first one right here.

In short: a macro is a saved set of actions you can run as often as you like, and in Excel it is VBA code. You either record one (Developer > Record Macro) or write it in the VBA editor (Alt+F11). Run it with Alt+F8, a shortcut of your own or a button on the sheet. Save the file as .xlsm. Macros run in Excel for Windows and Mac, but not in Excel for the web or on an iPhone or iPad.

Try it right now

Here is a macro at its smallest: one line that copies a value from A2 to B2. Type that line between Sub and End Sub. Run shows you the sheet afterwards, and Check also tries your macro with different text. No copy of Excel needed.

Task Write a macro CopyValue that copies the contents of A2 to B2. Check also tests it with different text in A2.

ABCD
1OriginalCopy
2Invoice 1043
3
4
5

What is a macro?

Microsoft's own definition is short: an action, or a set of actions, that you can run as many times as you want. In Excel, every macro is a small program written in VBA (Visual Basic for Applications), the language built into the desktop versions of Excel. That includes recorded macros. The recorder turns what you do into VBA code, which you can then open, read and change in the VBA editor.

So when people say "VBA and macros", they mostly mean the same thing from two angles. The macro is the procedure, say Sub CleanUpExport(), and VBA is the language it is written in.

Uses for macros at work

According to Microsoft, nearly anything you can do with the mouse, the keyboard or a dialog box can also be done in VBA, and automating repetitive tasks is one of its main uses. Some everyday jobs that suit a macro:

  • Reshape an export: take the monthly download from your accounting or order system and give it the same treatment every time: drop columns, format the header, add a total row.
  • Clean up a list: delete blank rows, remove duplicates, trim stray spaces and fix the case of names.
  • Build sheets: one sheet per region, team or customer, filled with the matching rows.
  • Combine: pull the sheets from several branches onto one summary sheet.
  • Get a file ready to send: turn formulas into plain values so the recipient only sees results.
  • React to changes: stamp the date next to a row as soon as someone edits it.

Working code for these tasks and more is on the macro examples page. One piece of advice comes from Microsoft itself: before you start, ask whether a built-in feature does the job more simply. If a formula or a filter solves it in one step, you do not need a macro.

Making a macro: record it or write it

Both routes start on the Developer tab, which is hidden at first. On Windows, go to File > Options > Customize Ribbon and tick Developer under Main Tabs.

RecordWrite
HowDeveloper > Record Macro, enter a macro name and, if you like, a shortcut key, click OK, do the steps, then Stop RecordingOpen the VBA editor with Alt+F11, insert a module and type the code
You neednothing beyond Excelbasic VBA
LimitsPlays back exactly the steps you recordedCan make decisions ("if the cell is empty, delete the row") and handle lists of any length
Learn it hereRecord a macroYour first macro in the editor

Microsoft calls the recorder a great way to learn VBA, while warning that the code it produces can be confusing. A common pattern is to record, read what came out, then tidy it up. The recorder lesson walks you through that.

Running a macro

OptionHow
Macro dialog boxAlt+F8 or Developer > Macros, pick the macro, Run. On a Mac, Option+F8.
Your own shortcutSet it while recording, or later under Developer > Macros > Options. On Windows that is Ctrl+letter, or Ctrl+Shift+letter for a capital. Careful: while the workbook is open, it replaces any built-in Excel shortcut on the same keys.
Button on the sheetDeveloper, Controls group, Insert, then Button under Form Controls. Click on the sheet and choose the macro in Assign Macro.
Shape or pictureRight-click it and choose Assign Macro.
Quick Access Toolbar or ribbonAdd the macro as a button to the toolbar, or to a custom group such as "My Macros" on a ribbon tab.
From the VBA editorClick inside the macro and press F5.
When the workbook opensA Workbook_Open procedure in the workbook's own code (ThisWorkbook) runs automatically each time the file is opened.

Turning on macros

When you open a file that contains macros, they start out switched off. Excel shows a Security Warning bar with an Enable Content button, which is what the default setting, Disable VBA macros with notification, does. Only click Enable Content for your own files or ones whose macros you know. You can still view and edit the workbook without enabling anything.

If the file came from the internet or an email attachment, Excel on Windows blocks its macros outright and shows a Security Risk banner with no button to enable them. What each message means, how to unblock your own files and which Trust Center settings make sense are covered in Enable macros and save them safely.

Saving: .xlsm, not .xlsx

A regular .xlsx workbook cannot store VBA code. To keep your macros, go to File > Save As and pick Excel Macro-Enabled Workbook (.xlsm) in the Save as type list. If you want a macro in every workbook, store it in the Personal Macro Workbook when you record it. Excel keeps it in a hidden file called Personal.xlsb and opens it in the background whenever Excel starts.

Where macros run

ExcelVBA macros
Excel for Windowsyes
Excel for Macyes, with some differences, for example no ActiveX controls. See VBA on a Mac.
Excel for the webno: you cannot create, run or edit macros there. They do stay in the file, and Open in Desktop App lets you carry on in the desktop version.
Excel on iPhone and iPadno

For Excel in the browser, Microsoft offers a separate tool called Office Scripts. When VBA is the right choice and when something else fits better is the topic of comparing ways to automate Excel.

Learning to write macros

The VBA course takes you in six levels from your first macro to reports across several sheets. Every exercise runs in the browser: you write the code, see the sheet before and after, and get errors worded the way the VBA editor words them. Level 1 is free. For an overview of every lesson, see Learn VBA, and to experiment without a task, open the VBA online editor.

FAQ

Is there a keyboard shortcut for turning on macros?

Not in Microsoft's list of Excel shortcuts. You turn macros on with Enable Content in the Security Warning bar, or for good by keeping the file in a trusted location. The two shortcuts worth knowing are Alt+F8 for the Macro dialog (run, edit or delete) and Alt+F11 for the VBA editor.

What is the difference between VBA and macros in Excel?

VBA is the programming language, and a macro is a program written in it. Every Excel macro is VBA code, including the ones you record.

Can I use one macro in all my workbooks?

Yes. When recording, set Store macro in to Personal Macro Workbook. Excel opens that workbook every time it starts, so its macros are available wherever you work.

Are macros dangerous?

Macros can do far more than change cells, which is why attackers use Office files with macros to spread malware. Microsoft's advice is never to enable macros unless you know what they do. Your own macros you can always read in the VBA editor.

Do macros work with Microsoft 365?

Yes, in the desktop apps of Excel for Windows and Mac; VBA is built in and needs no extra license. Open the same file in Excel for the web and the macros will not run there.

Can I practice macros without installing Excel?

Yes. The exercises on LearnThisNext run on our server in a simulated workbook, on a tablet or phone too. For macros in your own files you will need Excel for Windows or Mac later on.

Up next: your first macro in the VBA editor

How a macro is put together, where it lives in Excel and how to start it, with three exercises you solve in the browser.

Start the first lesson

Write to us

Choose language

Select your preferred language