How to Macro in Excel: Record, Edit, and Run VBA

VBA & macros · Updated November 23, 2026

To macro in Excel, you record a series of actions using the Macro Recorder, or write them in VBA (Visual Basic for Applications) using the Visual Basic Editor. The fastest way to start is to enable the Developer tab, click “Record Macro,” perform your steps, then click “Stop Recording” — Excel stores the actions as VBA code you can run anytime.

What Is an Excel Macro?

A macro is a set of instructions that automates repetitive tasks in Excel. When you record a macro, Excel translates your mouse clicks and keystrokes into VBA code. When you run the macro, Excel replays those actions exactly. Macros can be as simple as formatting a range or as complex as building a full reporting dashboard. They are stored inside the workbook file, usually in a module, and can be triggered by a button, a keyboard shortcut, or by running them from the Macros dialog.

Enabling the Developer Tab

Before you can record or edit macros, you need the Developer tab on the ribbon. It is hidden by default.

  1. Right-click anywhere on the ribbon and select Customize the Ribbon.
  2. In the right-hand list, check Developer and click OK.

On Mac, go to Excel > Preferences > Ribbon & Toolbar, then check Developer under the Main Tabs list. The Developer tab gives you access to the Macro Recorder, the Visual Basic Editor, and macro security settings.

Recording Your First Macro

The Macro Recorder is the easiest way to create a macro without writing code. Here’s how to record one that formats a header row:

  1. On the Developer tab, click Record Macro.
  2. Give the macro a name (no spaces, must start with a letter) and optionally assign a shortcut key like Ctrl+Shift+H.
  3. Choose where to store it: This Workbook keeps it in the current file; Personal Macro Workbook makes it available in all your workbooks.
  4. Click OK, then perform the actions you want to record — for example, select the header row, make it bold, add a fill color, and increase font size.
  5. Click Stop Recording on the Developer tab.

Your macro is now saved. To run it, press the shortcut you assigned, or go to View > Macros > View Macros, select the macro, and click Run.

Editing Macros in the Visual Basic Editor

Recording is great for simple tasks, but editing the code gives you more control. Open the Visual Basic Editor by pressing Alt+F11 (Windows) or Option+F11 (Mac). In the Project Explorer, expand the module that contains your macro (usually Module1). Double-click it to see the code. For example, a recorded macro that bolds a header might look like this:

Sub FormatHeader()
    Range("A1").Select
    Selection.Font.Bold = True
    With Selection.Interior
        .Color = 65535
    End With
End Sub

You can modify the range, change colors, or add loops. Always save a copy of your workbook before running edited code, because macros cannot be undone with Ctrl+Z.

Running Macros and Assigning Buttons

You can run a macro from the Macros dialog, but for frequent use, assign it to a button. On the Developer tab, click Insert and choose a Button (Form Control). Draw the button on the sheet, then in the Assign Macro dialog, select your macro and click OK. Now clicking the button runs the macro. Alternatively, you can assign a macro to a shape or an image by right-clicking it and selecting Assign Macro.

Macro Security and File Formats

Macros can contain harmful code, so Excel restricts them by default. When you open a workbook with macros, you may see a yellow bar with an Enable Content button. To change security settings, go to File > Options > Trust Center > Trust Center Settings > Macro Settings. The recommended setting is Disable all macros with notification — this lets you choose when to enable them. Also, to save a workbook with macros, you must use the .xlsm file extension (not .xlsx). If you save as .xlsx, Excel will strip out the macros.

Common Problems and How to Fix Them

  • Macro not running: Check that macros are enabled and the workbook is saved as .xlsm.
  • “Cannot run the macro” error: The macro may be in a different workbook. If it’s in the Personal Macro Workbook, that file must be open.
  • Relative vs. absolute references: By default, the recorder uses absolute references (it records the exact cells you select). If you want the macro to work on any selected range, use Relative Reference on the Developer tab before recording.
  • Macro works on wrong sheet: The recorder records the active sheet. To make it always target a specific sheet, edit the code to include Worksheets("Sheet1").Range("A1") instead of just Range("A1").

Frequently Asked Questions

Can I undo a macro after running it? No, macros cannot be undone with Ctrl+Z. Always save your workbook before running a macro, especially one that deletes or overwrites data.

What is the Personal Macro Workbook? It’s a hidden workbook named PERSONAL.XLSB that stores macros you want available in all your workbooks. When you choose “Personal Macro Workbook” during recording, Excel creates it and loads it at startup.

How do I delete a macro? Open the Visual Basic Editor, find the module containing the macro, and delete the entire Sub…End Sub block. Or go to View > Macros, select the macro, and click Delete — but this only works for macros in the current workbook, not in the Personal Macro Workbook.

Why does my macro run slowly? The recorder often selects cells unnecessarily. Turning off screen updating with Application.ScreenUpdating = False at the start of your code and setting it back to True at the end can speed things up significantly.

Can I record a macro that works on different data each time? Yes. Use Relative Reference mode before recording so the macro acts relative to the active cell. For more flexibility, you can edit the code to use variables and loops.

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.