Excel VBA: What It Is, How It Works, and When to Use It

VBA & macros · Updated November 11, 2026

Yes—desktop Microsoft Excel includes VBA, or Visual Basic for Applications, which lets you automate tasks and create macros. Excel for the web does not run or edit VBA macros; if you mean a yes-or-no prompt, VBA can also display one and respond to the answer.

Is VBA included in Excel?

VBA is built into the desktop versions of Excel for Windows and Mac, including Microsoft 365 desktop Excel. You do not usually need to install a separate programming language to write or run a macro. VBA code is stored with the workbook or in a personal macro workbook, and Excel runs it when you trigger the macro or an event set up in the code.

VBA is different from a worksheet formula. A formula calculates a result in a cell, while VBA can perform actions such as changing cell contents, formatting ranges, creating sheets, or asking the user a question. It can work with the Excel object model: the collections and objects that represent workbooks, worksheets, ranges, charts, and other parts of Excel.

One important qualification is that “Excel” does not mean every way of using Excel. The desktop application supports VBA, but the browser-based Excel for the web does not execute, create, or edit VBA macros. Excel for the web can open many macro-enabled workbooks and preserve their VBA content, but you must use desktop Excel to work with the macros themselves.

What VBA does and how macros work

A macro is a set of instructions that Excel can run. VBA is the language commonly used to write those instructions. For example, a macro might loop through rows and flag overdue invoices, copy a report into a new workbook, or ask whether a user wants to mark a task complete. Unlike a formula that recalculates when its inputs change, a macro generally runs in response to a user action or a programmed event.

Excel includes a macro recorder that can capture some actions and translate them into VBA. Recording is a useful introduction: select Record Macro, perform a sequence of formatting or editing steps, and stop recording. The resulting code can often be adjusted in the Visual Basic Editor. However, the recorder may produce repetitive code and cannot capture every kind of decision or interaction. For branching logic, loops, or a customized prompt, writing or editing VBA is more suitable.

VBA can interact with files, the operating system, and other Office applications, depending on the code and permissions involved. That flexibility is useful for automation, but it is also why macros deserve more scrutiny than an ordinary formula. A workbook’s VBA is not automatically safe simply because it is an Excel file.

Windows, Mac, and Excel for the web

On Windows, the VBA Editor is commonly opened with Alt+F11. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon, then select Developer. The Developer tab provides access to Visual Basic, Macros, and Record Macro.

On Mac, desktop Excel supports VBA, although some features and integrations differ from Windows. The Developer tab can be enabled through Excel > Settings > Ribbon & Toolbar. Option+F11 commonly opens the Visual Basic Editor; depending on the keyboard, you may also need to hold Fn. A Windows-specific automation feature or reference may not work on Mac, so code that depends on other applications or system components should be checked on the target platform.

Excel for the web has no VBA editor and cannot run VBA macros. If a workbook relies on a macro, open it in the desktop app to run or modify that code. This distinction matters when sharing a file with people who use different devices: the workbook’s data may be viewable in a browser even though its automation is unavailable there.

What people use Excel VBA for

VBA is most useful when a task is repetitive, follows predictable rules, and involves actions that are awkward to perform manually. Common examples include:

  • Applying the same formatting and layout to a recurring report.
  • Cleaning imported data by removing blank rows, standardizing text, or moving fields.
  • Creating worksheets or workbooks from a list of departments, clients, or dates.
  • Validating entries and showing a prompt when a user makes a choice.
  • Running a sequence of workbook operations with one button or menu command.

For example, a monthly report might arrive with the same columns but inconsistent formatting. A macro could set column widths, apply a date format, add a heading, and save a prepared copy. It is worth designing the code around clear assumptions—such as which sheet contains the data and where the headers are—rather than relying on whichever cell happens to be selected.

VBA is not automatically the best tool for every spreadsheet problem. A one-time calculation may be easier with a formula. Reusable transformations may fit Power Query, while newer automation in Microsoft 365 may be possible with Office Scripts. The right choice depends on the workflow, user environment, and features required.

Making a yes-or-no prompt with VBA

If “yes no” refers to asking a user a question, VBA’s MsgBox function can display buttons and return the choice. This example writes the selected answer into the currently active cell. Select the intended cell before running it, because the macro changes that cell.

Sub AskYesNo()
    Dim answer As VbMsgBoxResult

    ' Ask the user to choose Yes or No.
    answer = MsgBox("Mark this task complete?", vbYesNo + vbQuestion, "Confirm")

    ' Write the choice to the active cell.
    If answer = vbYes Then
        ActiveCell.Value = "Yes"
    Else
        ActiveCell.Value = "No"
    End If
End Sub

To try it, first save a copy of the workbook. In desktop Excel, open the Visual Basic Editor, insert a standard module, and paste the code. Return to Excel, select the target cell, and run the macro from Developer > Macros > Run, or run it from the editor. The prompt offers Yes and No; choosing either writes that word into the selected cell. Macros cannot be undone with Ctrl+Z, so save a copy before running code that changes workbook contents.

If you only need a user to choose Yes or No in a cell, a dropdown is usually simpler than VBA. Select the cells, then use Data > Data Validation, choose List as the validation type, and enter Yes,No as the source. This keeps the choice in the worksheet without requiring a macro or macro-enabled workbook. A formula can then respond to the selection: with the answer in A2, enter =IF(A2="Yes","Approved","Not approved") in B2. If A2 contains Yes, B2 displays Approved; otherwise it displays Not approved.

Creating, saving, and running a macro

A practical first workflow is to record a small action, inspect the generated code, and then decide whether to edit or replace it. In desktop Excel for Windows, use Developer > Record Macro, enter a name, perform the actions, and select Stop Recording. Macro names cannot contain spaces. The macro can be run from Developer > Macros, assigned to a suitable control, or called by other VBA code.

When saving a workbook that contains VBA, choose Excel Macro-Enabled Workbook (*.xlsm). The standard .xlsx format does not store VBA macros, so saving a macro workbook as .xlsx can remove its macro content. If you want code available in future workbooks on that computer, Excel also has a Personal Macro Workbook option; it is stored locally and is not automatically included when you send another workbook to someone else.

When someone else receives an .xlsm file, they may need to open it in desktop Excel and allow macros according to their organization’s security settings. A macro may not run simply because the file contains code: Excel’s security controls, file source, and administrative policies can affect whether it is allowed.

Security and safe use

VBA can make changes and, depending on the code, perform actions beyond editing cells. Treat an unexpected macro-enabled file as potentially risky, especially if it came from an untrusted sender or asks you to enable content without a clear reason. Do not enable macros just to view ordinary worksheet data if you do not trust the source.

On Windows, macro controls are available through File > Options > Trust Center > Trust Center Settings > Macro Settings. The safest choice depends on your organization’s policy and the source of the workbook. Avoid enabling all macros globally as a convenience. If a legitimate workbook is blocked, confirm its origin and follow your organization’s approved process rather than changing security settings broadly.

Before running code you wrote or received, inspect what it does and work on a copy. Pay particular attention to code that deletes sheets or files, changes many cells, sends information elsewhere, or accesses external applications. Since Ctrl+Z does not reverse macro actions, a separate backup is a practical safeguard, not an optional nicety.

VBA compared with formulas, Power Query, and Office Scripts

Use a worksheet formula when the goal is to calculate or display a result from cell values. Formulas are visible in the sheet, recalculate as inputs change, and generally work in Excel for the web. A Yes/No dropdown with an IF formula is a good example of a simple decision that does not require VBA.

Power Query is designed to import, clean, combine, and reshape data through repeatable transformation steps. It is often a better fit than a macro for recurring data preparation, especially when the process is about transforming tables rather than automating clicks or interacting with workbook elements. Its availability and exact capabilities can differ by platform and Excel edition.

Office Scripts are another automation option in supported Microsoft 365 environments, particularly for web-based workflows and integration with Power Automate. They use TypeScript rather than VBA and are not a drop-in replacement for every desktop macro. If coworkers need browser access, check that the chosen automation runs in their environment. If the workflow depends on existing VBA, desktop Excel remains necessary.

Common problems and what to check

  • The Developer tab is missing: Enable it in Excel’s ribbon settings. On Windows, use File > Options > Customize Ribbon; on Mac, use Excel > Settings > Ribbon & Toolbar.
  • The macro is not listed: Check that the workbook was saved as .xlsm, that the code is in a standard module if it should appear in the macro list, and that you are looking in the correct workbook.
  • The macro is blocked: Confirm that you are using desktop Excel and check the workbook’s source and applicable security policy. Excel for the web cannot run VBA, regardless of the file extension.
  • The macro works on one computer but not another: Look for Windows-only references, external applications, file paths, or permissions that differ between machines. Test on the same platform and Excel setup used by the people who will run it.
  • Changes cannot be reversed: VBA actions do not provide the normal Ctrl+Z undo history. Restore a backup or use a versioned copy rather than expecting Undo to recover the prior workbook.

Frequently Asked Questions

Does Excel have VBA?
Yes. Desktop Excel for Windows and Mac includes VBA support. Excel for the web does not run or edit VBA macros.

Is VBA free with Excel?
VBA is included as part of supported desktop Excel applications; it is not normally a separate programming-language purchase. Access to Excel itself depends on the license or plan you use.

Can Excel for the web run an .xlsm macro?
No. The web app cannot execute or edit VBA. Open the file in desktop Excel to run or change its macros; browser access to the workbook does not make the VBA available.

How do I enable VBA in Excel?
Enable the Developer tab in Excel’s ribbon settings, then use Visual Basic or Macros. If a macro is blocked, review the source and security policy rather than turning on all macros indiscriminately.

Can I make a Yes/No choice without VBA?
Yes. Use Data > Data Validation to create a Yes,No dropdown, then use a formula if the workbook needs to respond to the choice. This is often easier to share and safer than a macro for a simple selection.

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.