Microsoft VBA for Excel is the programming language built into desktop Excel for automating repetitive tasks, creating custom procedures, and interacting with workbook data. In Excel for Microsoft 365 on Windows, you can start with Developer > Code > Record Macro or open the VBA editor with Alt+F11; VBA macros do not run in Excel for the web.
What VBA Is—and What an Excel Macro Means
VBA stands for Visual Basic for Applications. It is included with desktop versions of Microsoft Office applications, including Excel, and lets you write instructions that work with Excel’s workbook, worksheet, cell, chart, and other objects. A macro is a procedure that carries out a set of actions. Many Excel macros are written in VBA, although people sometimes use “macro” to mean the recorded actions or the code that runs them.
A macro might format a monthly report, copy information from several sheets into a summary, or check a list for missing values. Instead of repeating the same clicks each time, you run the procedure. VBA can also make decisions and repeat steps, which means it can handle tasks that a basic recorded macro cannot express well.
VBA is not the same thing as an Excel formula. A formula calculates a value in a worksheet cell; VBA is code that can change workbook content, move data, display a message, or control a sequence of operations. It is also not a separate Excel product that you need to install, though the Developer tab may be hidden until you turn it on.
How VBA Works in Excel
Excel exposes features through an object model: a structured set of objects and properties that VBA code can refer to. For example, a workbook contains worksheets, and a worksheet contains ranges of cells. A line of code can refer to a range and change its value, formatting, or other properties. Procedures are generally stored in modules inside a workbook or in a separate personal macro workbook.
A typical VBA procedure begins with a Sub line and ends with End Sub. The code between them is the sequence Excel follows when the procedure runs. It can refer to a specific sheet, use a loop to process many rows, or use an If statement to take different actions depending on the data. VBA can also respond to events, such as a user opening a workbook or changing a cell, when event code is set up in the appropriate workbook or worksheet module.
The Visual Basic Editor is where you view and edit this code. You can run a procedure from the editor, assign it to a button, or select it from Excel’s macro list. A macro runs with the permissions and access available to Excel, so code should be treated as executable software rather than as harmless worksheet text.
Getting Started: Record or Write a Macro
For a first experiment in Excel for Microsoft 365 on Windows, turn on the Developer tab by choosing File > Options > Customize Ribbon, selecting Developer in the list of Main Tabs, and choosing OK. The tab contains controls for recording, editing, and running macros. You can also open the editor directly with Alt+F11.
To record a simple procedure, choose Developer > Code > Record Macro, enter a name, and choose where to store it. Then perform the actions you want Excel to record and choose Developer > Code > Stop Recording. The recording captures many mouse and keyboard actions as VBA statements. It is useful for learning the code behind a task, but the result may be less flexible than code written to handle changing data or different worksheet layouts.
- Choose This Workbook to keep the macro in the current workbook.
- Choose Personal Macro Workbook if you want a macro available when you use Excel on that computer. This personal file is separate from the workbook you share.
- Use the macro name field to give the procedure a useful name. Macro names cannot contain spaces.
To inspect or edit code, open the Visual Basic Editor and choose Insert > Module in its menu to add a standard code module. The editor’s Project pane shows the open workbooks and their modules. If the pane is hidden, the editor’s View menu can display it. A recorded macro is a practical starting point: edit a copy first, because a small change can affect which cells the code targets.
A Small VBA Example
This example assumes the active worksheet has a header row in cells A1:B1. It makes the text in those two cells bold; it does not change the cell values. In the Visual Basic Editor, put the procedure in a standard module and run it while the intended worksheet is active.
Option Explicit
Sub FormatHeaderRow()
' Use the worksheet that is active when the macro runs.
Dim ws As Worksheet
Set ws = ActiveSheet
' Emphasize the two header cells.
ws.Range("A1:B1").Font.Bold = True
End Sub
The variable ws represents the worksheet, and Range(“A1:B1”) identifies the two cells. Using ActiveSheet is convenient for a short demonstration, but it means the result depends on which sheet is selected at run time. For a shared or important workbook, code that explicitly identifies the intended worksheet can be safer, provided that the sheet name is correct. Before running any macro that changes a workbook, save a separate copy: macro actions cannot be reversed with Ctrl+Z.
Saving Macro-Enabled Workbooks
A workbook containing VBA needs a macro-capable file format if you want to preserve its code. In Excel, use File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). The ordinary .xlsx format does not store VBA projects. If you save a workbook with macros as .xlsx, Excel warns that the VBA project will be removed if you continue in that format.
Use .xlsm when you need both the workbook content and its macros in one file. Excel add-ins and other specialized formats can also contain VBA, but they serve different purposes. A file extension is a useful clue, not proof that a file is safe: an .xlsm file may contain benign automation, unwanted code, or code you do not understand. A workbook without macros can still contain risky links or other content, so file safety involves more than the extension.
Macro Security and Safe Use
Macros can make extensive changes to a workbook and may interact with files or other applications. That power is useful, but it is why Excel warns about macros in files from untrusted sources. Do not enable macros simply because a document asks you to. Confirm who sent the file, whether you expected it, and whether the sender can explain what the macro does.
On Windows, macro options are available through File > Options > Trust Center > Trust Center Settings > Macro Settings. The available choices depend on the Office configuration and organizational policy. Avoid setting Excel to enable all macros globally. In managed workplaces, an administrator may control these settings. Digitally signed macros can help identify a publisher and show whether signed code has changed, but a signature is not a guarantee that the code is harmless.
- Keep an untouched backup before running unfamiliar code, especially if it edits or deletes data.
- Review code or ask a trusted technical person to review it if you do not know what it will do.
- Do not treat a macro downloaded from the internet as trustworthy just because Excel can open it.
- Use a test copy of a workbook when learning or debugging, and check the output before sharing it.
Windows, Mac, and Excel for the Web
VBA is available in desktop Excel for Windows and Mac, though some features and integrations can differ between the platforms. The Windows Visual Basic Editor shortcut is Alt+F11. On Mac, Option+F11 may open it; depending on the keyboard and function-key settings, you may need Fn+Option+F11. The Developer tab is available in desktop Excel for Mac, and ribbon settings are managed through Excel > Preferences > Ribbon & Toolbar in versions that use that preference path.
Excel for the web can open and edit many macro-enabled workbooks, but it cannot create, run, or edit VBA macros. The workbook’s VBA project is not executed in the browser. If you need to run the macro, open the file in a supported desktop version of Excel. A web user can work with the workbook’s cells without running its VBA, so do not assume that a browser session will perform steps normally handled by a macro.
VBA Compared with Office Scripts and Other Options
VBA is often a good fit for automating an existing desktop workbook, working with Excel’s desktop object model, or maintaining a process that already depends on macros. It can be powerful, but it is tied to desktop Excel and can be difficult to share safely when recipients have different platforms, policies, or versions.
Office Scripts are Microsoft’s alternative for automating Excel on the web and are designed to work with cloud-based workflows, including Power Automate. They use TypeScript rather than VBA, and they are not simply VBA code translated into a different editor. Availability depends on the Microsoft 365 plan, account, and administrator settings. Power Query may be a better choice when the core task is repeatedly importing, cleaning, combining, and refreshing data rather than controlling Excel through a sequence of actions. For a one-off calculation or transformation, ordinary formulas or built-in Excel features may be easier to maintain than any script.
- Choose VBA when the task depends on desktop Excel automation or existing VBA code.
- Consider Office Scripts when the task needs browser-based automation or a cloud workflow and your account supports it.
- Consider Power Query for repeatable data import and reshaping.
- Use formulas or standard Excel tools when they solve the task clearly without code.
Common Limitations and Troubleshooting
Recorded macros often refer to fixed cell addresses, selected ranges, or the currently active sheet. If rows are inserted or the report changes shape, the macro may act on the wrong cells. Before sharing a macro, check assumptions such as worksheet names, header locations, and whether the data starts in the same row each time. Explicit references and clear comments can make code easier to maintain, but they cannot replace testing with realistic workbook layouts.
If a macro does not appear in the macro list, check that it is stored in the workbook you expect and that it is a runnable procedure in a standard module. A procedure declared Private or code placed in a worksheet event module may not appear like a normal macro. If Excel blocks a macro, review the file’s source and your organization’s security policy rather than weakening security settings indiscriminately. If code stops with an error, the highlighted line and error message can point to a missing sheet, an invalid range, or another assumption that was not met.
Frequently Asked Questions
Is VBA included with Excel?
VBA is included with desktop Excel for Windows and Mac; it is not a separate programming-language purchase. Excel for the web does not run VBA macros.
How do I enable VBA in Excel?
In Excel for Windows, show the Developer tab through File > Options > Customize Ribbon and select Developer. Use Developer > Visual Basic to open the editor, or press Alt+F11.
Can Excel for the web run VBA macros?
No. Excel for the web can work with many macro-enabled workbooks, but it cannot run, create, or edit the VBA code. Use desktop Excel to run a VBA macro.
Will saving as .xlsx keep my macros?
No. The .xlsx format does not preserve a VBA project. Save the workbook as Excel Macro-Enabled Workbook (*.xlsm) to keep its macros.
Is VBA difficult to learn?
You can record a basic macro without writing code, but useful, reliable automation usually requires learning procedures, variables, ranges, and error handling. Starting with a small task and a copy of the workbook makes the learning process safer.
Related
- Excel Zoom Shortcuts: How to Zoom In and Out Quickly
- Excel Shortcut Keys for Switching Between Worksheets
- Excel Shortcut Keys for the Next Sheet in a Workbook
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Import CSV to Excel Using VBA: A Safe, Repeatable MethodTo import a CSV file into Excel with VBA, use Workbooks.OpenText to parse the file, then copy its…
- Excel VBA Get Current Workbook Name: Code and TipsTo get the name of the workbook currently active in Excel VBA, use ActiveWorkbook.Name. For example, assign Set…
- Excel VBA: How to Check AutoFilter StatusIn Excel VBA, check Worksheet.FilterMode to find out whether a worksheet currently has a filter applied, and check…
- VBA Dictionary Count: How to Get the Number of KeysTo get the number of keys in a VBA Dictionary, read its Count property: dict.Count. It returns the…
- VBA Basic Error Handling: Patterns, Examples, and TipsVBA basic error handling uses On Error to decide what a procedure should do when a runtime error…
- Using MsgBox in VBA: Syntax, Buttons, and Return ValuesIn Excel VBA, use MsgBox to show a pop-up message or ask a user a simple question. Call…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.