MS Office Macros: What They Are & How to Use Them

VBA & macros · Updated November 22, 2026

MS Office macros are small programs written in Visual Basic for Applications (VBA) that automate repetitive tasks in Microsoft Office applications like Excel, Word, and PowerPoint. In short, a macro records your actions (or you write code) and then replays them with a single click or keyboard shortcut, saving time and reducing errors.

What Exactly Is a Macro?

A macro is a sequence of instructions that Office executes automatically. When you record a macro in Excel, for example, every mouse click, keystroke, and menu selection is translated into VBA code. That code is stored inside the workbook file (with an .xlsm extension) or in a personal macro workbook so it is available across all your files. In Word, macros can automate formatting, inserting boilerplate text, or applying styles. In PowerPoint, they can manipulate slides, shapes, and animations programmatically.

Macros are not the same as simple keyboard shortcuts (like Ctrl+C for copy). Shortcuts trigger built-in commands; macros can run a custom series of commands and even make decisions based on conditions, loop through data, and interact with other Office applications.

How Macros Work: Recording vs. Writing Code

There are two main ways to create a macro:

  • Record a macro: Go to View > Macros > Record Macro in Excel or Word (on Windows). Perform the actions you want to automate, then click Stop Recording. Office writes the VBA code for you.
  • Write VBA code manually: Open the Visual Basic Editor (Alt+F11 in Windows, Fn+Alt+F11 on Mac) and type the code. This gives you full control—you can add loops, conditionals, and user prompts.

Recorded macros are great for simple, linear tasks (e.g., formatting a header, applying a filter). For complex logic—like “if cell A1 is empty, then copy from B1, else delete row”—you need to write or edit the VBA code yourself.

Why Use Macros? Real-World Examples

People use macros to eliminate hours of manual work. Common examples include:

  • Excel: Automatically cleaning imported data—removing duplicates, trimming spaces, and converting text to proper case.
  • Excel: Generating monthly reports by pulling data from multiple sheets and applying consistent formatting.
  • Word: Inserting a formatted signature block or a standard contract clause with a single keystroke.
  • Excel: Automating repetitive calculations, like applying a complex formula to thousands of rows and then copying results to a summary sheet.
  • PowerPoint: Resizing and repositioning images across dozens of slides to match a template.

For example, a simple Excel macro to bold all cells in column A with a value greater than 100 might look like this (in a module):

Sub BoldLargeValues()
    Dim cell As Range
    For Each cell In Range("A1:A100")
        If cell.Value > 100 Then
            cell.Font.Bold = True
        End If
    Next cell
End Sub

Before running any macro, always save a copy of your workbook—macros cannot be undone with Ctrl+Z if they change data irreversibly.

Security Risks and How to Stay Safe

Macros can carry malware. Because VBA code can delete files, send emails, or modify system settings, cybercriminals often hide malicious macros inside Office documents. Microsoft has responded with security features:

  • By default, macros are disabled in files downloaded from the internet. You’ll see a yellow bar with an Enable Content button.
  • Office uses Mark of the Web to flag files from the internet; you must explicitly trust the file to enable macros.
  • You can set macro security levels under File > Options > Trust Center > Trust Center Settings &em; Macro Settings. Options range from disabling all macros to enabling all (not recommended).

To stay safe: only enable macros in documents from sources you trust, and never enable content in unsolicited email attachments. If a macro asks for unusual permissions (like accessing the file system or sending email), treat it with suspicion.

How to Choose Between Recording and Coding

For a beginner, recording is the fastest way to start. But recording has limitations: it captures exact cell references and fixed selections, so it may not work if your data range changes. For example, if you record a macro that formats A1:A10, it will not automatically adjust to A1:A20 next time. To make macros dynamic, you need to edit the code to use variables or the CurrentRegion property.

If you need a macro that works on any data set, writing code is better. Start with simple tasks, then learn to use loops (For Each), conditionals (If...Then), and variables. The macro recorder is also a great learning tool: record a task, then open the VBA editor (Alt+F11) and study the generated code.

Common Problems and Troubleshooting

New macro users often hit these issues:

  • Macro not running: Check that macros are enabled, and that the file is saved with a macro-enabled extension (.xlsm, .docm, .pptm). If you save as .xlsx, macros are stripped.
  • “Compile error” or “Sub or Function not defined”: This usually means a typo in the code or a missing reference (e.g., a library that isn’t installed). Check the code line by line.
  • Macro works on my PC but not on a colleague’s: Different Excel versions or security settings can cause issues. Also, if you use relative references (recorded with Use Relative References on), the macro behaves differently based on the active cell.
  • Accidentally deleted data: Always test macros on a copy of your data first. Use Application.Undo is not available for macros; you must include your own undo logic or save a backup.

Alternatives to Macros

Macros aren’t the only way to automate Office. Consider these alternatives:

  • Excel formulas and functions: For simple transformations, formulas like TRIM, UNIQUE (in Excel 365), or IF can do the job without VBA.
  • Power Query (Excel): For data import and cleaning, Power Query (Data > Get & Transform) is more robust and doesn’t require code.
  • Office Scripts (Excel for the web): A newer, JavaScript-based automation tool for Excel online. It’s less powerful than VBA but safer and easier to share.
  • Built-in features: Word’s Quick Parts and AutoText, or Excel’s Table styles, can handle many repetitive formatting tasks.

Choose macros when you need cross-application automation, complex logic, or interaction with the user (like input boxes). For simple, data-only tasks, other tools are often simpler and safer.

Frequently Asked Questions

Are macros the same as add-ins?

No. A macro is a single procedure or set of procedures stored in a module. An add-in is a file (usually .xlam) that loads automatically and can contain multiple macros, custom functions, and user forms. Add-ins are meant to be shared and installed, while macros are usually personal or document-specific.

Can macros work in Excel for the web?

No, Excel for the web does not support VBA macros. Instead, it uses Office Scripts, which are written in JavaScript and run in the browser. If you open a workbook with macros in the web version, the macros won’t run, but the data remains intact. You can edit the file in the desktop app to use macros.

How do I share a macro with someone else?

You can share the entire macro-enabled workbook (.xlsm), or you can export the module from the VBA editor (File > Export File) and have the other person import it into their own workbook. Alternatively, you can create an add-in (.xlam) and share that. Always ensure the recipient trusts the source and enables macros only if they know what the code does.

Will a macro slow down my Office application?

Macros can slow down performance if they are poorly written—for example, if they loop through thousands of cells one by one. To improve speed, you can turn off screen updating (Application.ScreenUpdating = False) and calculation (Application.Calculation = xlCalculationManual) at the start of the macro, then restore them at the end. Well-written macros run quickly.

Can I undo a macro?

Generally, no. The Undo stack (Ctrl+Z) is cleared when a macro runs. That’s why it’s critical to save a backup copy before running any macro that modifies data. Some macros can include custom undo functionality, but that requires advanced coding and is not automatic.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.