Excel VBA Macro: Create, Run, and Save Your First Macro

VBA & macros · Updated December 2, 2026

To create a VBA macro in Excel for Microsoft 365 on Windows, enable the Developer tab, choose Developer > Visual Basic, insert a module, and type a VBA procedure between Sub and End Sub. Run it with the editor’s Run command or Excel’s Macros dialog, then save the workbook as an Excel Macro-Enabled Workbook (.xlsm).

What a VBA macro does

VBA, or Visual Basic for Applications, is Excel’s built-in programming language for automating workbook tasks. A macro is a procedure that carries out instructions, such as formatting a report, cleaning data, or copying values. It can save time when the same sequence of actions needs to be repeated, and it can do more than a recorded macro when you add conditions, loops, or variables.

Excel’s macro recorder can capture many actions without requiring you to write code. However, recorded code may include unnecessary selections and may not adapt well to changing data. Writing a small procedure yourself is often clearer and easier to maintain.

Show the Developer tab and open the editor

The Developer tab is hidden by default in many Excel installations. To show it in Microsoft 365 for Windows:

  1. Choose File > Options.
  2. Select Customize Ribbon.
  3. Under Main Tabs, check Developer, then select OK.
  4. Choose Developer > Visual Basic to open the Visual Basic for Applications (VBA) editor.

You can also open the editor with Alt+F11 on Windows. On a Mac, enable Developer from Excel > Preferences (or Settings, depending on the release) > Ribbon & Toolbar, then use Developer > Visual Basic. The exact preference labels can vary between Mac versions. Excel for the web does not create, edit, or run VBA macros; open the workbook in the desktop Excel app instead.

Write a first macro

In the VBA editor, select your workbook in the Project Explorer. If that pane is not visible, use View > Project Explorer. Choose Insert > Module, then enter this example in the blank code window:

Sub FormatHeader()
    ' Format the first row of the active worksheet.
    Dim headerRange As Range

    Set headerRange = ActiveSheet.Rows(1)
    headerRange.Font.Bold = True
    headerRange.Interior.Color = RGB(217, 225, 242)
    headerRange.Columns.AutoFit
End Sub

This procedure makes the first row bold, applies a light-blue fill, and adjusts column widths. It uses ActiveSheet, so it acts on whichever worksheet is active when the macro runs. For a safer, more predictable macro, specify the intended worksheet by name, for example with ThisWorkbook.Worksheets("Report"), and confirm that the sheet exists.

VBA statements run from top to bottom. Sub FormatHeader() names the procedure, Dim declares a variable, and End Sub marks its end. The apostrophe starts a comment, which explains the code but does not execute. Indenting statements is optional but makes nested code easier to read.

Run and test the macro

Before running code that changes a workbook, save a copy. Macros cannot be undone with Ctrl+Z, and a mistaken command can alter many cells at once. To run the example, return to Excel and select Developer > Macros, choose FormatHeader, and select Run. Alternatively, place the cursor inside the procedure in the VBA editor and choose Run > Run Sub/UserForm or press F5.

Test on a small, disposable workbook first. Check that the intended sheet is active and that the macro changed only the expected cells. To inspect the code one line at a time, use the VBA editor’s debugging tools, such as Step Into, rather than repeatedly running a procedure that may make cumulative changes.

Save the workbook and manage macro security

A workbook saved as .xlsx cannot store VBA code. To retain a macro, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). The .xlsm format keeps the workbook’s sheets and VBA project together. If you save a macro-enabled file as .xlsx, Excel warns that the macro project will be removed if you continue.

Excel may block macros from files downloaded from the internet or received from an untrusted source. Only enable content when you trust the file and understand what it will do. Do not lower macro security globally just to run an unfamiliar workbook. In an organization, security settings may be managed by an administrator. A digitally signed macro can help identify its publisher, but a signature is not a substitute for trusting the source and reviewing what the code does.

Record a macro or write VBA?

Use the macro recorder when you want Excel to generate a starting point for straightforward actions, such as applying a number format or setting up a print layout. In Windows Excel, choose Developer > Record Macro, perform the actions, and select Stop Recording. Excel’s recorder is also available in desktop Excel for Mac, though some actions and interface details differ.

Use the VBA editor when the task needs decisions, repetition, or clearer control over which cells are affected. You can inspect recorded code in the editor and simplify it. Neither approach works as VBA in Excel for the web. If your main goal is automating a Microsoft 365 workflow in the browser, Office Scripts may be a better fit, but they use TypeScript rather than VBA and have different capabilities and setup.

Frequently Asked Questions

Do I need to know how to code to make an Excel macro?
You can record a macro without writing code. To adapt it reliably or handle changing data, some VBA basics are useful. Start with one small task and learn how to refer to workbooks, worksheets, ranges, and cells.

Why is my macro not listed in Excel?
Check that the code is a public procedure beginning with Sub and without arguments, and that it is in a standard module rather than a worksheet or workbook code module. Also confirm that the file is open and that macros are permitted by your security settings.

Can I run a VBA macro in Excel for the web?
No. Excel for the web can open and edit many macro-enabled workbooks, but it cannot create, edit, or run VBA. Use desktop Excel to work with VBA, or consider Office Scripts for supported browser-based automation.

How do I add a button to run a macro?
In desktop Excel for Windows, choose Developer > Insert and select a Button under Form Controls. Draw the button on the worksheet, select the macro in the Assign Macro dialog, and choose OK. On Mac, available controls and menu details can differ; use the Developer tab’s controls if present.

Can I undo a macro with Ctrl+Z?
Usually not. Running VBA clears Excel’s normal undo history, and its changes cannot be reversed with Ctrl+Z. Save a separate copy before running a macro, especially if it edits, deletes, or moves data.

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.