Tutorials

What is a macro in Excel, and how to enable one safely

A macro is a recorded or written set of steps Excel can repeat for you. Here is what that means in practice, how to run one, and how to tell whether it is safe to enable.

TEPtThe ExcelToolkit Pro teamEditorial4 min read
Post on XLinkedIn
The Visual Basic Editor in Excel showing a short commented macro

A macro is a set of instructions Excel can carry out for you, as many times as you like, at the press of a button. If you do the same twenty clicks every Monday, a macro can do them in a second. To get a commented, ready-to-paste macro for a common job, use the VBA Macro Generator.

What a macro actually is

Under the hood, a macro is a small program written in VBA (Visual Basic for Applications), the programming language built into Excel and the other Office desktop apps. It can do almost anything you can do with the mouse: select cells, type values, delete rows, format, create sheets, save files. It can also do things the mouse cannot, like loop through ten thousand rows checking each one.

Here is a complete macro. It deletes every empty row on the current sheet:

Sub DeleteBlankRows()

Dim r As Long For r = ActiveSheet.UsedRange.Rows.Count To 1 Step -1 If Application.WorksheetFunction.CountA(Rows(r)) = 0 Then Rows(r).Delete Next r

End Sub

You do not need to understand every word to see the idea: go through the rows from the bottom up, and delete the ones that are empty.

Two ways to create one

Record it. On the Developer tab (or View → Macros → Record Macro), Excel watches what you do and writes the VBA for you. Recording is the fastest way to automate a fixed sequence of formatting steps. Its weakness is that it records exactly what you clicked, including the cell addresses, so a recorded macro often breaks when the data is a different size next time.

Write it. Press Alt + F11 (Mac: Option + F11) to open the Visual Basic Editor, choose Insert → Module, and type or paste the code. Written macros can make decisions, loop and adapt to the data, which is why most useful macros are written or at least edited by hand.

If the Developer tab is not visible: File → Options → Customize Ribbon, then tick Developer.

How to run a macro

  1. Press Alt + F8 (Mac: Option + F8) to see the macros in open workbooks.
  2. Pick one and press Run.
  3. To make it a button, insert a shape, right-click it and choose Assign Macro.

One rule matters more than any other: a macro cannot be undone with Ctrl + Z. Save a copy of the workbook before you run a macro that deletes or overwrites anything.

Saving a workbook with macros

A normal .xlsx file cannot hold macros. If you add code and save as .xlsx, Excel warns you and then throws the code away. Save as Excel Macro-Enabled Workbook (.xlsm) instead. For macros you want in every workbook, Excel offers a hidden Personal Macro Workbook (choose it as the location when you record).

Why Excel blocks macros, and how to enable them safely

Macros are powerful, which is exactly why they have been used to spread malware for decades: a macro can download and run a program as easily as it can delete a row. So Excel is cautious.

  • Files from the internet or email are blocked by default. Windows marks downloaded files with a "came from the internet" flag, and current versions of Microsoft 365 refuse to run macros in such files, showing a red "Microsoft has blocked macros from running" bar instead of an Enable button.
  • Other files show a yellow Security Warning bar with an Enable Content button.

To enable macros in a file you trust:

  1. Close the file.
  2. In File Explorer, right-click it → Properties.
  3. On the General tab, tick Unblock and press OK.
  4. Open it again and press Enable Content on the yellow bar.

For files you use regularly, a better option is a Trusted Location: File → Options → Trust Center → Trust Center Settings → Trusted Locations. Macros in files stored there run without asking. Keep that folder for your own files only.

How to tell whether a macro is safe

Before enabling anything someone sent you, ask:

  • Did you expect it? An unexpected invoice or "updated payroll" workbook that asks you to enable content is the classic attack.
  • Can you read the code? Press Alt + F11 and look. Be suspicious of code that is deliberately unreadable, very long strings of random characters, or anything mentioning Shell, CreateObject("WScript.Shell"), URLs or PowerShell.
  • Does it run on opening? Macros named Auto_Open or Workbook_Open run as soon as content is enabled.

The Excel File Inspector lists the macros, auto-run names and external links inside a workbook without opening it in Excel, which is a quick first check.

When not to use a macro

Many jobs people write macros for have safer tools now. Reshaping data, combining files from a folder and cleaning columns are often better done in Power Query, which records steps you can refresh and does not need macro security at all. The Power Query Helper writes those steps for you. Keep macros for things only code can do: acting on many sheets or files, or responding to edits as they happen.

TEPt

The ExcelToolkit Pro team

Editorial

We build the browser-based tools on this site, and we write these guides from the problems that actually come through the contact form — broken lookups, workbooks that will not open, exports that arrive mangled. Everything here is tested against real files before it is published.

Get the next one by email

New guides every other Tuesday. Unsubscribe in one click.

No spam. Unsubscribe in one click.

Keep reading

Multiple workbook files being consolidated into a single combined spreadsheet
Tutorials3 min read

How to merge hundreds of workbooks without writing a macro

Monthly branch reports, regional exports, one file per client. Here is how to consolidate them reliably, including the header-mismatch problem nobody warns you about.

TEPtThe ExcelToolkit Pro teamEditorial
An Excel column chart of monthly sales beside the table it was built from
Tutorials4 min read

How to create charts in Excel (and pick the right one)

A chart takes about ten seconds to insert and much longer to get right. The steps, which chart fits which question, and the fixes for the problems everyone hits.

TEPtThe ExcelToolkit Pro teamEditorial