Excel Macro Commands List: Common VBA Actions Explained

VBA & macros · Updated November 2, 2026

Excel Macro Commands List: Common VBA Actions Explained

An Excel macro command is a Visual Basic for Applications (VBA) instruction that tells Excel what to do, such as write a value, format cells, or save a workbook. There is no single complete, fixed command list: VBA provides language statements and Excel objects with many properties and methods. The common commands below cover the actions most spreadsheet macros perform.

What “macro command” means in Excel

People use “macro command” to mean a few related things. A recorded macro is VBA code created as Excel records clicks and keystrokes. A VBA statement is an instruction in that code, such as declaring a variable or starting a loop. An Excel method or property is an operation or setting applied to a workbook object. For example, a macro can use the Range object to refer to cells, a method to clear their contents, and a property to set their font.

As a result, a useful command list is a reference to common building blocks rather than an exhaustive catalog. Excel’s object model includes objects for workbooks, worksheets, ranges, charts, tables, and more. Each object supports its own methods and properties, and the available options depend on what you are automating.

The examples here are for desktop Excel for Microsoft 365 on Windows. VBA macros can also run in Excel for Mac, though some operating-system features and interface steps differ. Excel for the web can open macro-enabled workbooks, but it cannot create, edit, or run VBA macros.

Common VBA commands and what they do

These are common statements, methods, and properties you will encounter in recorded or hand-written Excel macros. The exact syntax depends on the workbook and the task.

  • Sub and End Sub mark the beginning and end of a macro procedure. A procedure is the unit you can run from Excel’s macro list.
  • Dim declares a variable, such as a worksheet reference or a row number. Declaring variables makes code easier to check and maintain.
  • Set assigns an object, such as a worksheet, to an object variable. It is used for objects, not ordinary values such as text or numbers.
  • Range refers to a cell or group of cells by address, such as A1 or A1:D20. Cells refers to a cell by row and column numbers.
  • Worksheets and Workbooks refer to sheets and open workbooks. ThisWorkbook means the workbook containing the VBA code; ActiveWorkbook means the workbook currently active in Excel.
  • Value reads or assigns a cell’s value. Formula reads or assigns a formula. Use ClearContents to remove cell contents while retaining formatting.
  • Font, Interior, and Borders expose formatting properties. For example, a macro can set a font’s bold property or a cell’s fill color.
  • If...Then...Else makes a decision; For...Next repeats instructions a set number of times; and For Each...Next repeats them for each item in a collection.
  • MsgBox displays a message. InputBox asks a user to enter text. These can add basic interaction, though they are not substitutes for a carefully designed form.
  • Save saves changes to a workbook. Close closes a workbook. A macro using either should be written carefully so it does not save or close the wrong file.

A practical formatting macro

This example formats a sales table on a worksheet named Sales. It assumes headers are in row 1, the table occupies columns A through D, and column A contains an entry on every data row. Before running a macro, save a copy of the workbook: VBA actions cannot be undone with Ctrl+Z.

Sub FormatSalesReport()
    Dim ws As Worksheet
    Dim lastRow As Long

    ' Work on the Sales sheet in the workbook containing this code.
    Set ws = ThisWorkbook.Worksheets("Sales")

    ' Find the last nonempty row in column A.
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' Format the table range and emphasize its header row.
    With ws.Range("A1:D" & lastRow)
        .Borders.LineStyle = xlContinuous
    End With
    ws.Range("A1:D1").Font.Bold = True
End Sub

The macro uses ThisWorkbook so it targets the file that stores the code, rather than whichever workbook happens to be active. The End(xlUp) expression starts at the bottom of column A and moves upward to find the last populated cell. If column A has gaps or is not reliably filled, choose a column that consistently contains data or use a different way to determine the table’s extent. The code adds borders across the identified range and bolds the header; it does not change the data.

How to create and run a macro

In Windows desktop Excel, show the Developer tab if it is not already visible: choose File > Options > Customize Ribbon, then check Developer. On the Developer tab, choose Record Macro to capture actions, or choose Visual Basic to open the VBA editor and write code. A recorded macro is a useful starting point, but it may include unnecessary selections and may refer to fixed cell addresses.

  1. Save the workbook in a macro-enabled format, usually Excel Macro-Enabled Workbook (*.xlsm), if it needs to retain VBA code.
  2. Choose Developer > Visual Basic, then insert a standard module from the editor’s Insert menu if you are adding a simple procedure.
  3. Paste or write the procedure in the module. Check that the sheet names and ranges in the code match the workbook.
  4. Return to Excel and choose Developer > Macros, select the macro, and choose Run. You can also run it from the VBA editor.

On Mac, the Developer tab and VBA editor are available in desktop Excel, but the menus and keyboard shortcuts differ from Windows. Excel for the web does not provide the VBA editor or run VBA procedures, so use desktop Excel for these steps. A workbook stored as .xlsx cannot retain VBA code when saved in that format; choose .xlsm to keep the macro.

Choosing the right reference and target

Many macro mistakes come from an unclear target. A reference such as Range("A1") without a worksheet qualifier refers to the active sheet. If the user changes sheets, the same instruction may affect a different location. A qualified reference such as ws.Range("A1") makes the intended worksheet explicit, as in the example above.

Likewise, ActiveWorkbook and ThisWorkbook are not interchangeable. Use ThisWorkbook when the macro should operate on the file containing the code. Use ActiveWorkbook only when the macro is deliberately intended to work on the workbook the user has selected. In automation involving several files, identify and qualify each workbook and worksheet so an instruction cannot silently act on the wrong one.

For cell references, Range("B2") is convenient when the address is known. Cells(rowNumber, columnNumber) is useful when row or column numbers are calculated in code. Both can refer to a single cell or be combined to construct a range. Avoid relying on Select and Activate unless the task genuinely requires selecting something; direct references are generally clearer and less dependent on the current screen state.

Recorded macros versus hand-written VBA

Recording is a practical way to discover how Excel represents a sequence of actions. For example, recording a font change can show which properties Excel uses. The resulting code may be verbose, however, and often includes selecting cells before acting on them. Those selections can make a macro fragile if the active sheet, selection, or workbook differs from the recorded situation.

Hand-written VBA is better when the task needs decisions, repetition, or dynamic ranges. A loop can process many sheets, and an If statement can apply an action only when a condition is met. A recorded macro can still be useful as a first draft, especially for unfamiliar formatting or chart operations; refine it by adding explicit worksheet references and removing unnecessary selection steps.

For recurring spreadsheet tasks, also consider whether a macro is needed at all. Excel formulas, tables, built-in sort and filter tools, and Power Query may be easier to share and maintain for data transformation. VBA is most helpful when you need to automate a sequence of Excel actions or interact with workbook elements that are awkward to manage with formulas alone.

Common problems and how to avoid them

  • Macro is missing after saving: The file may have been saved as .xlsx, which does not preserve VBA projects. Save a copy as .xlsm when the code must remain in the workbook.
  • Excel blocks the macro: Macro security settings may prevent code from running. Enable macros only for files you trust; do not lower security protections simply to run an unknown file.
  • Subscript out of range: A workbook or worksheet name in the code may not exactly match an available object. Check spelling, spaces, and which workbook the macro is targeting.
  • Wrong cells change: Unqualified references may act on the active worksheet or workbook. Qualify references and test on a copy with representative data.
  • Run-time error on a range: A range may not exist in the expected sheet, or assumptions about the data may be false. Check the workbook layout and consider what should happen when there are no data rows.
  • Undo does not reverse the macro: Excel’s normal undo history does not restore changes made by VBA. Save a backup before running code that edits, deletes, moves, or overwrites data.

When debugging, the VBA editor can pause on an error and show the line that failed. Read the highlighted instruction and verify the object, sheet name, and range it refers to. Test with a small copy of the workbook before applying a macro to important files.

Safety, sharing, and alternatives

Macros can make broad changes quickly, so treat VBA files as executable code rather than passive documents. A macro from an unknown source could alter or delete workbook data, or perform other unwanted actions. Use files from trusted sources, review code when possible, and follow your organization’s security rules. Protected View or macro security prompts should not be bypassed casually.

When sharing a macro-enabled workbook, tell recipients what the macro does, what workbook or sheets it expects, and whether it changes data. A short instruction sheet and a clear button label can prevent accidental runs. If recipients only need the finished results, consider sharing a copy without macros or exporting the appropriate output instead.

Alternatives depend on the task. Formulas recalculate values as inputs change. Power Query is designed to import and reshape data through repeatable steps. Office Scripts can automate tasks in supported Microsoft 365 environments, but they are a different technology from VBA and have their own availability and capabilities. Choose based on the users’ Excel platform, the task, and how the solution will be maintained.

Frequently Asked Questions

What is the command to run a macro in Excel?
In desktop Excel for Windows, choose Developer > Macros, select the procedure, and choose Run. You can also run a procedure from the VBA editor. Excel for the web cannot run VBA macros.

What is the most common command in an Excel macro?
There is no single most common command, because macros do different jobs. Range is a frequently used way to refer to cells, while methods and properties on ranges perform actions such as assigning values or changing formatting.

Can I undo an Excel macro?
Usually not with Excel’s Ctrl+Z undo command. VBA actions generally clear the normal undo history, so save a separate copy before running a macro that changes data or formatting.

Do Excel macros work in the browser?
No. Excel for the web can open and edit many workbooks that contain VBA, but it cannot create, edit, or run those VBA macros. Open the file in desktop Excel to use its VBA macro.

Which file type keeps an Excel macro?
Use .xlsm for a standard macro-enabled workbook. Saving a workbook containing VBA as .xlsx removes the VBA project from the saved file.

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.