What macros do, and how to open them safely
Record and run macros10 min
Builds on Save, file types and AutoSave.
Every Monday, a payroll assistant at a hotel group in Cork turns the timesheet export into something the managers can read. She bolds the headings, sets the dates and hours to one format, widens the columns and sorts the rows by department. It takes 15 minutes, with the same clicks every week.
How it works
A macro is a list of steps that Excel carries out for you. You can record one: Excel writes down what you do as code in VBA (Visual Basic for Applications), the programming language built into desktop Excel. Run the macro next Monday, and it repeats the steps in a second or two.
A job is worth a macro when it comes back every week or month with the same steps, such as formatting an export or building the same summary.
The tools for macros are on the Developer tab, which Excel hides until you turn it on. On Windows, choose File > Options > Customize Ribbon and tick Developer under Main Tabs.
Keep macros in a .xlsm file
A standard workbook (.xlsx) can't hold macros. Save a workbook that has them as Excel Macro-Enabled Workbook (*.xlsm). If you save it as .xlsx anyway, Excel asks first, and choosing Yes saves the file without its macros.
Open macro files safely
A macro can do almost anything you can do on your computer, so a workbook from the wrong sender can carry harmful code. Excel opens a file's macros switched off and tells you with a bar under the ribbon.
| Row | A | B |
|---|---|---|
| 1 | What you see | What to do |
| 2 | A yellow bar, SECURITY WARNING, with an Enable Content button | Select Enable Content only for a file you expected, from someone you trust. Excel then trusts that file. |
| 3 | A red bar, SECURITY RISK, with no Enable Content button | Excel for Windows blocked the macros because the file came from the internet. Leave them blocked unless the sender confirms they sent it. |
Windows marks files that come from the internet, such as downloads and email attachments, with what Microsoft calls the Mark of the Web. Once the sender confirms a marked file, save it to your computer, right-click it in File Explorer, select Properties, tick Unblock on the General tab and select OK, then open it again. IT teams can also set up trusted locations, folders whose files open with their macros on.
The yellow bar comes from Excel's default setting, Disable VBA macros with notification, under File > Options > Trust Center > Trust Center Settings > Macro Settings. Leave it there. Enable VBA macros lets every macro in every file run without asking, and Excel labels it not recommended.
Common mistakes
- Enabling content because the sheet tells you to. A file that hides its contents until you enable macros is a common trick in harmful workbooks.
- Switching on Enable VBA macros so the bars go away. Every file you open could then run code without asking.
In short
- A macro repeats a list of steps, written in VBA, in Excel for Windows or Mac.
- Keep a workbook with macros as .xlsm.
- Enable content only in files you expected from people you trust, and unblock a file from the internet only after the sender confirms it.
Practice
5 exercises · free, no account needed