The Excel macro editor is the Visual Basic Editor (VBE), the built-in environment for viewing, writing, and debugging VBA code. In Excel for Microsoft 365 on Windows, open it with Alt+F11 or choose Developer > Code > Visual Basic; if the Developer tab is missing, enable it in Excel Options first.
How to open the Excel macro editor
On Windows, Alt+F11 opens the VBE from Excel. You can also open the Developer tab and select Visual Basic in the Code group. If you do not see Developer, go to File > Options > Customize Ribbon, check Developer in the list of main tabs, and select OK. This changes the ribbon display; it does not enable macros or change the workbook’s security settings.
On a Mac, use Tools > Macro > Visual Basic Editor. Depending on the keyboard and its function-key settings, Option+F11 may also work; you may need to press Fn as well. The menu command is the dependable route if the shortcut is intercepted by macOS or a keyboard setting. Excel for the web does not include the VBA editor, so use the desktop app to create or edit VBA.
The editor works with the currently open workbooks. If you open it without a workbook, or the workbook you want is not open, its project may not appear in the editor’s project list. Return to Excel, open the relevant file, and then reopen or check the VBE.
What the editor does—and what a macro is
A macro is a set of instructions that automates actions or calculations in Excel. VBA, short for Visual Basic for Applications, is the programming language commonly used for Excel macros. The VBE is where you inspect and edit that code; it is separate from the worksheet grid and from Excel’s ribbon.
You do not have to start by programming. Excel’s macro recorder can capture many actions and produce VBA code you can examine in the editor. Recorded code is useful for learning or repeating straightforward formatting and navigation tasks, but it may be less adaptable than code written to handle changing data or exceptions. Recording a macro is not the same as making a permanent built-in Excel feature: the resulting code is stored in a workbook or another macro-enabled file.
VBA can read and change workbook contents, format cells, create reports, and interact with other Office features. That power is also why code should be treated carefully: a macro can make changes quickly, and a mistaken or malicious macro may affect files or data.
Understanding the main VBE windows
When the VBE opens, its Project Explorer lists open workbook projects. A project typically contains objects for the workbook and its worksheets, plus folders such as Modules. The workbook object and worksheet objects are useful places for code that responds to events, such as opening a workbook or changing a cell. A standard module is usually a clearer home for a general-purpose macro that you run on demand.
Double-click an object or module to open its code window. If you do not see Project Explorer in the VBE, choose View > Project Explorer or press Ctrl+R on Windows. The Properties window displays settings for the selected object; View > Properties Window or F4 opens it. The Immediate window, available from View > Immediate Window or Ctrl+G on Windows, can be used to inspect values or issue commands while debugging. These shortcuts are for the Windows VBE; on Mac, use the View menu if a shortcut differs.
The code window is where you read or type VBA. A procedure usually begins with a line such as Sub Report() and ends with End Sub. The Project Explorer helps you identify where that procedure is stored, while the editor’s menus provide commands for running, stepping through, and checking code.
Create and run a simple macro
To create a general-purpose macro, first open the workbook in desktop Excel. In the VBE, select the workbook project in Project Explorer, then choose Insert > Module. A blank code window appears. The following example writes a message to cell A1 on a worksheet named Sheet1:
Sub WriteMessage()
' Put a short message in cell A1 on Sheet1.
Dim target As Range
Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
target.Value = "Hello from VBA"
End Sub
This procedure assumes the workbook containing the code has a worksheet whose tab name is Sheet1. If the sheet has a different name, change the text inside the worksheet reference to match. The code changes a cell value, so save a copy of the workbook before running any macro you are learning from or do not fully understand. VBA changes cannot be undone with Ctrl+Z.
To run the procedure, place the cursor inside it in the VBE and use Run > Run Sub/UserForm, or return to Excel, select Developer > Code > Macros, choose WriteMessage, and select Run. Check A1 on Sheet1 for the result. In Excel, Developer > Code > Record Macro starts a recording; use Developer > Code > Stop Recording when finished. You can then inspect the recorded procedure in the VBE.
Save macros in the right file format
A standard .xlsx workbook cannot store VBA macro code. If you create or edit a macro in a workbook, use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm) to keep the code with the file. Excel may warn that macros cannot be saved in a macro-free workbook if you try to save VBA in .xlsx format; choosing to continue without macros removes the code.
Other formats can also support VBA, including Excel Binary Workbook (*.xlsb) and Excel Macro-Enabled Template (*.xltm), but .xlsm is a familiar choice for a workbook that contains macros. A macro-enabled file does not automatically run its code just because it is opened. Excel applies security controls, and users may need to allow macros for a trusted file. Save changes after editing in the VBE so the code is written into the workbook.
For code intended to be available in many workbooks on one computer, Excel also has a Personal Macro Workbook, commonly named PERSONAL.XLSB. It opens in the background when Excel starts after it has been created. This can be convenient for personal routines, but it is not a good way to distribute a macro to colleagues: the code is on that computer rather than packaged in the shared workbook.
Macro security and safe use
Only enable macros in files from a source you trust and when you understand why the code is needed. A macro can edit or delete workbook data, and VBA can perform actions beyond the visible sheet. On Windows, macro controls are under File > Options > Trust Center > Trust Center Settings > Macro Settings. The exact warnings a user sees can depend on the file’s origin, organizational policies, and Excel security configuration. Do not lower security settings simply to make an unfamiliar file run.
Excel for Windows blocks VBA macros in many files marked as coming from the internet. An unexpected request to enable content is a reason to verify the sender and the purpose of the file, not a reason to click through automatically. Organizations may also set policies that users cannot change. For recurring internal files, ask the organization’s IT team about approved signing or trusted-location procedures rather than adding an unverified folder as trusted.
Before running code that changes a workbook, make a separate copy. Unlike a normal cell edit, macro actions cannot be reversed with Excel’s Undo command, and the undo history may be cleared when a macro runs. For important data, test a macro on a disposable copy first and check the result before using it on the original.
Common problems and how to fix them
- Alt+F11 does nothing: Try Developer > Code > Visual Basic. On a laptop, the function-key row may require Fn. On Mac, use Tools > Macro > Visual Basic Editor.
- The Developer tab is missing: On Windows, enable it at File > Options > Customize Ribbon. The tab is hidden by default in some Excel setups.
- The code window or workbook project is missing: Open Project Explorer with View > Project Explorer. Confirm the correct workbook is open and expand its project. If the VBA project is protected, access may be restricted; do not try to bypass a password without authorization.
- A macro does not appear in the Macros dialog: Check that the procedure is a public, parameter-free macro in a standard module and that the active workbook contains it. Procedures declared Private or designed to take arguments are not listed as ordinary runnable macros.
- The macro stops with an error: Read the error message and inspect the highlighted line. Common causes include a misspelled worksheet name, a missing workbook, or a reference to a range that does not exist. Correct the cause, then run the macro on a copy again.
- Excel says macros are disabled: Check that the file is trusted and that organizational policy permits macros. Do not select a setting that enables all macros just to bypass a warning.
VBA editor versus Excel for the web and Office Scripts
Excel for the web can open and edit many workbook formats, but it cannot create, run, or edit VBA macros. To work on VBA, open the file in desktop Excel for Windows or Mac. A workbook containing macros may still be viewed or edited in the web app, but the VBA code does not run there; use desktop Excel for tasks that depend on that code.
Microsoft 365 also offers Office Scripts in supported accounts and environments. Office Scripts are written in TypeScript and are designed for supported web-based automation workflows; they are not VBA and cannot simply be pasted into the VBE. Availability depends on the account, administrator settings, and service. If a task must work in both desktop Excel and a web workflow, check the requirements for each platform before choosing VBA or Office Scripts.
Frequently Asked Questions
Is the Excel macro editor the same as Visual Basic?
The Excel macro editor is the Visual Basic Editor, or VBE, which is included with desktop Excel. It hosts VBA projects and code; it is not the standalone Visual Basic development environment used for other kinds of software.
Can I open the VBA editor in Excel for the web?
No. Excel for the web does not provide the VBA editor or run VBA macros. Open the workbook in desktop Excel to inspect or change its VBA code.
Why can I see a macro but not edit it?
The VBA project may be locked with a password, or the file may be read-only or restricted by your organization. Check your permissions and ask the workbook’s owner or administrator for authorized access.
Will saving a workbook as .xlsx keep its macros?
No. The .xlsx format does not store VBA. Save a macro-containing workbook as .xlsm, or use another suitable macro-enabled format, if you need to preserve its code.
Can I undo a macro with Ctrl+Z?
Usually not. Excel’s Undo command cannot reverse a VBA macro’s changes, so save a separate copy before running code that edits data.
Related
- Data Validation Checks in Excel: Rules and Examples
- Data Validation Checklist for Excel: What to Check
- Conditional Formatting in Microsoft Project: What Works
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA If Then Examples for ExcelVBA’s If...Then statement runs code when a condition is true, and can choose an alternative with Else or…
- Excel Macro Type Mismatch: Causes and FixesAn Excel macro “Type mismatch” is VBA run-time error 13: code tried to use a value as a…
- Dim VBA String: How to Declare and Use Text VariablesIn VBA, declare a text variable with Dim variableName As String, such as Dim customerName As String. The…
- Excel VBA: Check Whether a Dynamic Array Is AllocatedNo—VBA arrays are not objects, so If myArray Is Nothing is not a valid way to test one.…
- Access VBA InputBox Syntax and Practical Examples ExplainedIn Access VBA, use InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context]) to show a…
- Excel VBA Find Last Row: Reliable Methods and ExamplesIn Excel VBA, find the last row in one column with Cells(Rows.Count, "A").End(xlUp).Row, replacing A with the column…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.