Excel Macro Language: VBA, Office Scripts, and More

VBA & macros · Updated November 13, 2026

Excel’s traditional macro language is Visual Basic for Applications (VBA), which automates tasks in the desktop app. Microsoft 365 also offers Office Scripts, which use TypeScript for automating supported tasks in Excel for the web and other environments. The right option depends on where a workbook runs, what it needs to do, and how it will be shared.

What VBA is and why Excel uses it

VBA is a programming language built into many desktop Microsoft Office applications, including Excel. In Excel, VBA code can work with the application’s object model: objects such as workbooks, worksheets, ranges, charts, and tables. A macro is a procedure that performs a sequence of actions, often by running VBA code. For example, a macro can format a report, copy selected data to another sheet, or create a chart.

VBA has been used in Excel for decades, so many organizations rely on existing VBA workbooks and add-ins. It is especially useful for automating desktop workflows, responding to workbook events, and interacting with Excel features that have no simple worksheet formula equivalent. VBA can also use Windows-specific features, but that can make a workbook less portable.

A macro is not a special kind of worksheet formula. Formulas calculate values in cells; VBA procedures carry out actions or calculations through code. A workbook can contain both. For instance, a formula may calculate a total, while a macro applies a report layout and prepares a print area.

How Excel macros work

A VBA macro is stored in a workbook or in another location, such as an Excel add-in or the Personal Macro Workbook. When the procedure runs, Excel executes its instructions in order. The code may read cell values, change formatting, create worksheets, or call other procedures. Macros can also run in response to events, such as opening a workbook or changing a cell, if the code is set up for that purpose.

Excel’s macro recorder can turn many actions into VBA code. It is useful for learning the names of objects and properties, or for automating a predictable sequence of clicks. However, recorded code often includes extra selections and fixed cell references. It may work only when the sheet has the same layout and selection as it did during recording. Editing the code can make it more reusable, but the recorder does not replace understanding what the macro will do.

VBA runs in desktop Excel, not in Excel for the web. A web user can open a workbook that contains VBA, but Excel for the web cannot create, edit, or run its VBA macros. The workbook must be opened in a compatible desktop version to use them. This distinction matters when a team expects everyone to work in a browser.

Recording and writing a macro

In Excel for Microsoft 365 on Windows, turn on the Developer tab through File > Options > Customize Ribbon, then select Developer. To record a basic macro, choose Developer > Record Macro, enter a name, and perform the actions you want Excel to record. Choose Developer > Stop Recording when finished. To run a saved macro, use Developer > Macros, select the macro, and choose Run.

To inspect or edit VBA, choose Developer > Visual Basic, or press Alt+F11 on Windows. In the Visual Basic Editor, a standard macro can be placed in a module using Insert > Module. On Mac, Excel also supports VBA, but shortcut keys and some available features differ; the keyboard shortcut may depend on the keyboard and function-key settings. Excel for the web does not provide the VBA editor.

Here is a small example that makes cells A1 through D1 on the active sheet bold and gives them a light blue fill:

Sub FormatReportHeader()
    ' Format the first row of the report.
    Dim ws As Worksheet
    Dim header As Range

    Set ws = ActiveSheet
    Set header = ws.Range("A1:D1")

    header.Font.Bold = True
    header.Interior.Color = RGB(221, 235, 247)
End Sub

The code assumes the report heading is in A1:D1. If the active sheet is not the report sheet, it will format the wrong cells, which is why a macro should be checked against the workbook layout before use. The example changes formatting only; it does not identify a heading or verify that the selected sheet contains a report.

Saving, running, and sharing macro-enabled workbooks

A workbook containing VBA code generally needs to be saved as an Excel Macro-Enabled Workbook, with the .xlsm extension. The standard .xlsx format does not store VBA projects; saving a macro-enabled workbook as .xlsx prompts Excel to remove the macros. Excel add-ins use other file formats, such as .xlam, when code is intended to be installed and reused separately from a particular workbook.

Before running a macro, save a copy of the workbook. Macro actions cannot be undone with Ctrl+Z, so mistakes may require restoring a backup or rebuilding changed data. To make a macro easier to share, document what it changes, state which sheet or range it expects, and test it on a copy with representative data. A macro that depends on a local folder path, a particular sheet name, or a Windows-only component may not work for another person.

Recipients also need a version of desktop Excel that supports the macro and must follow their organization’s security rules. A file extension alone does not make a macro safe or guarantee it will run. If the code is important to a business process, consider maintaining a clear source copy and having changes reviewed rather than relying on an undocumented workbook as the only record.

VBA versus Office Scripts

Office Scripts are a separate automation option, not a newer version of VBA. They use TypeScript, a language based on JavaScript, and are designed for supported Excel experiences such as Excel for the web. Users can record actions in the Automate tab and inspect or adjust the resulting script in the code editor. Availability can depend on the user’s Microsoft 365 plan, tenant settings, and organization policies.

VBA is usually the practical choice when a task already depends on desktop Excel automation, an existing VBA project, event procedures, or capabilities available through the VBA object model. Office Scripts can suit repeatable workbook tasks in supported cloud workflows, particularly when a process is intended to run in Excel for the web or alongside Power Automate. Their capabilities and interfaces differ, so one language’s code cannot simply be pasted into the other.

  • Choose VBA when the users work in desktop Excel and the workbook needs VBA-specific features or must maintain existing macro code.
  • Consider Office Scripts when the task fits the supported scripting model and browser-based or cloud automation is important.
  • Use neither automatically when a formula, a built-in Excel feature, or a simple template can solve the problem with less maintenance.

Macro security and safe use

Macros can change or delete workbook data, and code may do more than its description suggests. Treat macro-enabled files from unknown senders with caution. Do not enable macros merely because a document asks you to; first verify who sent the file and why the code is needed. In a workplace, follow the organization’s security guidance rather than changing security settings to bypass a warning.

Excel’s Trust Center includes macro-related security settings, but the available controls may be managed by an administrator. Windows may also mark files downloaded from the internet as coming from an untrusted source, which can affect whether macros run. Do not move an unfamiliar workbook into a trusted location just to suppress a warning: trusted locations are intended for files you have a reason to trust.

Safer development habits include reviewing the code, testing with noncritical copies, limiting what the macro changes, and keeping backups. If a macro requests access to external files, email, or other resources, understand why those permissions or actions are needed before proceeding. Security warnings are not proof that code is malicious, but they are a reason to assess its source and purpose.

Common macro problems

When a macro does not run, start by checking whether the workbook is open in desktop Excel and saved in a macro-enabled format. Then check whether macros are blocked by security settings or organization policy. If the macro appears in the workbook but refers to a missing sheet or range, its assumptions may no longer match the workbook’s current structure.

Other frequent issues include code acting on the active sheet instead of the intended sheet, recorded macros relying on a particular selected cell, and references to files or add-ins that exist only on the original author’s computer. A procedure may also stop at a line of code when it encounters an unexpected value or a missing object. The Visual Basic Editor can help identify the line where an error occurs; read the error message and inspect the relevant sheet names, ranges, and references.

For a recorded macro, check whether the code contains unnecessary Select or Activate steps and whether it assumes fixed row counts. For a shared workbook, test with the same desktop or web environment that recipients will use. A VBA macro that works on Windows may rely on an API or other Windows-specific feature that is not available on Mac.

When a macro is not the best tool

Automation is not always the simplest solution. If the goal is to calculate results from changing data, worksheet formulas may update more reliably than a macro that must be run manually. Excel tables, filters, PivotTables, conditional formatting, and built-in data tools can also handle many common tasks without code. These options are often easier for colleagues to understand and maintain.

Use a macro when the workflow involves repeated actions that are difficult to express with built-in features, or when it saves meaningful time and can be tested safely. Before building one, write down the input data, expected output, and conditions under which it should run. That small specification helps reveal whether a formula, a manual process, VBA, or Office Scripts is the best fit—and makes later troubleshooting much easier.

Frequently Asked Questions

Is Excel macro language VBA?
Yes. VBA, or Visual Basic for Applications, is Excel’s traditional language for desktop macros. Office Scripts provide a separate TypeScript-based option in supported Excel environments.

Is VBA the same as Visual Basic?
No. VBA is related to Visual Basic, but it is the version embedded in Office applications and designed to work with their object models. Code for another Visual Basic environment may need changes before it works in Excel VBA.

Can I run an Excel macro in a browser?
Excel for the web cannot run, create, or edit VBA macros. You can open a macro-enabled workbook online, but use desktop Excel to run its VBA. Office Scripts may be an alternative for supported browser-based automation.

Do I need to know VBA to record a macro?
No. The recorder can capture many actions without requiring you to write code. Knowing some VBA is useful when you need to adapt recorded steps, handle changing data, or diagnose why a macro behaves unexpectedly.

Why does my macro disappear after saving?
A common cause is saving the workbook in .xlsx format, which does not retain VBA code. Save a workbook that contains macros as .xlsm, and confirm the file format before closing it.

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.