In Excel, a macro is a recorded or written sequence of actions and commands that you can run to automate repetitive tasks. More formally, a macro is a set of VBA (Visual Basic for Applications) code that Excel executes when you trigger it, either by clicking a button, pressing a shortcut key, or running it from the Developer tab.
What a Macro Actually Is
At its core, a macro is a small program. When you record a macro, Excel translates your mouse clicks and keystrokes into VBA code. For example, if you record yourself applying bold formatting to cell A1, the macro stores the instruction Range("A1").Font.Bold = True. When you run that macro later, Excel executes that line of code instantly.
Macros are stored inside the Excel workbook itself, usually in a module. The file extension for a workbook that contains macros is .xlsm (macro-enabled workbook). If you save a macro-containing file as a regular .xlsx, Excel strips the macros out and warns you.
You can create macros in two ways: by recording them (the easiest method for beginners) or by writing VBA code manually in the Visual Basic Editor. Recording works well for simple, linear tasks. Writing code is necessary when you need loops, conditions, or interaction with other programs.
How to Create a Macro (Recorded)
The simplest way to define a macro in practice is to record one. Here are the steps for Microsoft 365 on Windows:
- Go to View > Macros > Record Macro. (On Mac, the path is Tools > Macro > Record New Macro.)
- Give the macro a name (no spaces, must start with a letter).
- Optionally assign a shortcut key (like Ctrl+Shift+M) and choose where to store it (usually “This Workbook”).
- Click OK and perform the actions you want to automate.
- Click Stop Recording (the square icon in the status bar or the same menu).
After recording, you can open the Visual Basic Editor by pressing Alt+F11 (Windows) or Fn+Option+F11 (Mac) to see the generated code. Even if you never write a line of code, you can edit the recorded code to tweak behavior.
Writing a Simple Macro in VBA
If you prefer to write a macro directly, open the VBA editor (Alt+F11), go to Insert > Module, and type a simple procedure. For example:
Sub SayHello()
' This macro displays a message box
MsgBox "Hello, world!"
End Sub
To run it, press F5 while the cursor is inside the subroutine, or close the editor and use View > Macros > View Macros to select and run it. The code above is a complete macro: it starts with Sub, ends with End Sub, and contains one statement.
Macros can also take arguments. For instance, a macro to format a specific cell might look like:
Sub FormatCell(cell As Range)
cell.Font.Bold = True
cell.Interior.Color = RGB(255, 255, 0)
End Sub
To call this from another macro, you would write FormatCell Range("A1"). This is how you build more complex automation.
How to Choose Between Recording and Writing
Deciding whether to record or write a macro depends on your task and your comfort with code.
- Record when the task is a one-off series of steps you can perform manually, like formatting a report or inserting headers.
- Write when you need to repeat the same action many times (loops), make decisions based on cell values (If statements), or handle errors.
- Record then edit is a common hybrid: record the basic steps, then open the code and add a loop or condition.
For example, if you want to bold every cell in column A that contains the word “urgent”, recording won’t help because you need a conditional check. You’d write something like:
Sub BoldUrgent()
Dim cell As Range
For Each cell In Range("A1:A100")
If InStr(cell.Value, "urgent") > 0 Then
cell.Font.Bold = True
End If
Next cell
End Sub
This loops through 100 cells and bolds only those containing “urgent”.
Common Problems and Safety Concerns
Macros can cause problems if not handled carefully. The biggest risk is that macros can contain malicious code. Because of this, Excel disables macros by default and shows a yellow bar warning you. You should only enable macros from sources you trust. If you download a workbook with macros from the internet, be cautious.
Another common issue is that macros cannot be undone with Ctrl+Z. If your macro deletes data or makes widespread changes, you can’t simply undo it. Always save a copy of your workbook before running a macro that modifies data. The VBA editor has no built-in undo, so a mistake can be costly.
Other frequent problems include:
- Relative vs absolute recording: By default, recording uses absolute references (it always goes to the same cell). You can toggle to “Use Relative References” in the Developer tab if you want the macro to act on whatever cell is selected.
- Macros not running: If you receive “Macros are disabled”, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and choose “Enable all macros” (not recommended for daily use) or add the file location to trusted locations.
- File type errors: If you save a macro workbook as .xlsx, Excel warns that macros will be removed. Use .xlsm instead.
Alternatives to Macros
Macros are not the only way to automate in Excel. For users who don’t want to deal with VBA, there are simpler alternatives:
- Excel Tables and Formulas: Many repetitive tasks can be replaced with dynamic formulas like
SUMIFSorXLOOKUP. These update automatically without any code. - Power Query: For data cleaning and transformation, Power Query (Get & Transform) lets you record steps in a user interface that can be refreshed. It’s more powerful than formulas and doesn’t require VBA.
- Conditional Formatting: If you’re formatting cells based on rules, conditional formatting is a built-in feature that doesn’t need a macro.
- Office Scripts: In Excel for the web, you can use Office Scripts (a TypeScript-based language) to automate tasks. They are similar to macros but run in the cloud and are safer.
These alternatives are often easier to maintain and less risky. However, macros remain the most flexible and widely supported automation method in desktop Excel.
Frequently Asked Questions
Can I run a macro on Excel for the web? No. Excel for the web does not support VBA macros. You can view and edit macro-enabled workbooks, but you cannot run or record macros. Use the desktop version or consider Office Scripts.
How do I delete a macro? Open the Visual Basic Editor (Alt+F11), find the module containing the macro in the Project Explorer, and delete the entire module or just the specific Sub procedure. Alternatively, go to View > Macros, select the macro, and click Delete (but this only works for macros not in a module? Actually, it works for any macro in the workbook).
Why is my macro not working on another computer? The most common reason is that macros are disabled on that computer due to security settings. Another reason is that the workbook is saved as .xlsx, which strips macros. Also, if the macro references a specific file path or add-in that doesn’t exist on the other computer, it will fail.
Can macros be used to create a custom function? Yes. You can write a User Defined Function (UDF) in VBA that acts like a built-in Excel function. For example, you could create a function that returns the number of words in a cell. UDFs are macros, but they are called from formulas, not run directly.
Is there a way to see the code of a recorded macro? Yes. Press Alt+F11 to open the VBA editor, then look in the Modules folder. Double-click the module that contains your macro (often named Module1) to see the code. You can also edit it there.
What is the difference between a macro and a subroutine? In VBA, a subroutine (Sub) is a block of code that performs an action. A macro is simply a subroutine that Excel can run from the user interface. All macros are subroutines, but not all subroutines are macros—some are called by other code.
Related
- Data Entry Operator WFH: Real Jobs, Pay & How to Start
- Balloon with Number 1 Birthday: Meaning & Ideas
- Split String in Java 8: Methods and Examples
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Loop Example: For, Do While, and For Each LoopsA VBA loop repeats a block of code, either a set number of times or until a condition…
- VBA Copy and Paste a File: Practical Code and Safe StepsTo copy a file with VBA in Excel for Microsoft 365 on Windows, use the FileCopy statement or…
- How to Make a Macro on Logitech: Full GuideLogitech macros let you bind a sequence of keystrokes, mouse clicks, or button presses to a single key…
- Excel VBA ArrayList: How to Use It and When to Choose ItAn Excel VBA ArrayList is a resizable, zero-based list that can hold items and provides methods such as…
- Excel VBA: Copy and Paste Selected Cells with a MacroTo copy selected cells with VBA, call the range’s Copy method and specify a destination cell, as in…
- VBA Access: Sort Records in Queries and FormsTo sort records with Access VBA, put an ORDER BY clause in the SQL used to open a…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.