Macro · software

Also known as: macro recorder

On this page 7

A macro is a stored sequence of commands that office software can replay on demand to automate a repetitive task, such as formatting a report, cleaning imported data or generating a standard document. In office applications, macros are created either with a macro recorder, which captures user actions, or by writing code in an embedded scripting language such as VBA, LibreOffice Basic or Google Apps Script.

Macros turn minutes of manual clicking into a single command, but because they are executable code embedded in documents, they are also a well-known security risk. Understanding both sides is essential for anyone who builds, distributes or opens macro-enabled files.

How a macro works inside office software

A macro works by calling the application’s object model, the programmable interface that exposes documents, worksheets, ranges, paragraphs and slides as objects with properties and methods. When the macro runs, an embedded interpreter executes each instruction in order, exactly as if a user had issued the commands through menus.

The macro recorder translates mouse clicks and keystrokes into code automatically. Recorded code is literal: it repeats the exact cell references and selections used during recording. For that reason, experienced users treat recordings as a starting draft and then edit the code to add loops, variables, conditions and error handling.

Each major office suite has its own macro language and runtime. The table below compares the most common environments.

SuiteMacro languageWhere code is storedRecorder available
Microsoft 365 desktop appsVBA (Visual Basic for Applications)Inside .xlsm, .docm or .pptm files, or add-insYes, in Word and Excel
Excel for the webOffice Scripts (TypeScript)User’s cloud storage, separate from the workbookYes, action recorder
LibreOfficeLibreOffice Basic, Python, JavaScriptInside ODF documents or user librariesYes, in Writer and Calc
Google WorkspaceGoogle Apps Script (JavaScript)Bound to a file or as a standalone projectYes, in Google Sheets
ONLYOFFICEJavaScript plugin APIInside the documentNo
Macro environments in major office suites

Recorded and written macros compared

Recorded and written macros differ mainly in flexibility and robustness. A recording is fast to create but brittle, while hand-written code adapts to changing data and can validate its inputs.

PropertyRecorded macroHand-written macro
Skill requiredNone beyond normal useBasic programming knowledge
Creation timeSeconds to minutesMinutes to days
Cell and range referencesFixed, absolute addressesDynamic, based on data size
LogicLinear sequence onlyLoops, conditions, functions
Error handlingNoneCan trap and report errors
MaintainabilityLow, verbose codeHigh when well structured
Recorded versus hand-written macros

A practical workflow combines both: record the steps once, inspect the generated code, replace fixed references with variables, and wrap the logic in a loop. This approach is a common route by which spreadsheet users learn VBA or Apps Script.

Typical uses of macros in office work

Macros are most valuable for frequent, rule-based tasks that follow the same steps every time. In spreadsheets they clean and reshape imported data, refresh pivot tables and export reports; in word processors they apply house styles, insert boilerplate text and extend mail merge; in presentation software they update charts and standardize slide layouts.

The decision table below helps judge whether a macro is the right tool or whether a built-in feature would serve better.

SituationRecommended choiceReason
Same multistep task repeated weeklyMacroSaves time and removes manual errors
Calculation that updates with dataFormula or functionRecalculates automatically without code
Importing and reshaping external dataPower Query or a data connectorRepeatable, auditable and needs no code
Workflow across several cloud servicesWorkflow automation platformHandles authentication and triggers centrally
One-off cleanup of a single fileManual editingBuilding a macro would take longer
When to use a macro and when to choose an alternative

Macros can also be triggered by events, such as opening a file, changing a cell or saving a document. Event-driven macros are powerful but harder to debug, because they run without an explicit user command.

Macro security and trust settings

Macros are a security concern because they can run arbitrary code with the user’s permissions: they can read and write files, download content and launch other programs. For years, macro malware delivered through email attachments was one of the most common initial access techniques in phishing campaigns.

Office vendors responded with layered defenses. Microsoft Office now blocks macros in files downloaded from the internet by default, using the Mark of the Web attribute, and administrators can enforce macro policies through group policy or cloud management.

ControlHow it worksRecommended setting
Default macro blockingDisables macros in files from untrusted sourcesKeep enabled for all users
Trusted locationsAllows macros only from approved foldersRestrict to managed, write-protected paths
Digital signaturesVerifies the publisher and integrity of codeAllow only signed macros from trusted publishers
Protected ViewOpens downloaded files in a read-only sandboxKeep enabled
Attack surface reduction rulesBlock macros from launching child processesEnable in managed environments
Common macro security controls

The safest rule for end users is simple: never enable content in an unexpected document, even when the file itself urges you to do so. Legitimate business macros should come from signed sources or trusted locations configured by the organization.

Limitations and common mistakes

Macros are tightly bound to a specific application and file format, which limits portability. A VBA macro does not run in Google Sheets, and saving a workbook as plain .xlsx silently removes all macro code.

  • Relying on selections makes code slow and fragile; working with objects directly is faster.
  • Hard-coded ranges break when the data grows; dynamic ranges or tables avoid this.
  • Missing error handling leaves files half-processed when an unexpected value appears.
  • Undocumented macros become unmaintainable when their author leaves the organization.
  • No version control means a broken edit cannot easily be rolled back.

Another limitation is performance: macros that update the screen or recalculate after every step can run very slowly on large files. Temporarily disabling screen updating and automatic calculation often shortens run time dramatically.

In everyday language

A macro is like teaching your computer a small routine. You show the program what to do once, or write the steps down, and from then on it repeats them for you with a single click or shortcut.

Imagine formatting the same weekly report every Monday: changing fonts, sorting columns and adding totals. A macro does all of that in seconds. The catch is that macros are small programs, so only run macros you trust, just as you would not install software from an unknown sender.

Seen in the real world

An accounts payable clerk at a mid-sized distributor receives a raw export of about 3,000 invoice lines from the purchasing system every morning. Preparing it by hand, deleting empty rows, converting text dates, adding supplier subtotals and highlighting overdue items, took roughly 40 minutes a day.

The clerk recorded the steps once with the macro recorder, then worked with a colleague to replace fixed ranges with a loop that detects the last row. The final macro is stored in a signed add-in in a trusted location. It now completes the preparation in under 10 seconds, removes copy errors and saves the team more than 140 hours a year.