The VBA Editor in Microsoft Excel (also called the Visual Basic Editor, or VBE) opens with the keyboard shortcut Alt + F11 on Windows or Option + F11 on a Mac. From there you can record a macro, read and edit its code, write your own procedures, and test them without leaving Excel. Nothing needs to be installed first, but on a Mac you must have the Visual Basic Editor installed (it is part of the standard Office for Mac installer) and, on both platforms, macros must not be blocked by policy. File and menu labels can differ slightly by Excel version, platform and region.

What You'll Need

  • A desktop copy of Excel (Microsoft 365, Excel 2021, 2019 or 2016) on Windows or Mac. The VBA Editor is not available in Excel for the web, and you cannot open it on iPhone, iPad or Android — those apps can run macros in some cases but cannot edit code.
  • A workbook saved in a macro-enabled format (.xlsm). If you save as .xlsx, Excel asks whether to keep macro-free format and your code is discarded on the next save.
  • Permission to run macros. In Windows Excel, File > Options > Trust Center > Trust Center Settings > Macro Settings controls this; in organisations the setting is often locked by the administrator.
  • On a Mac, the Visual Basic Editor component included with the Office for Mac installer, and the Developer tab enabled if you want point-and-click tools.

Step-by-Step

  1. Save the workbook as macro-enabled first. Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm) from the file type list. Doing this before you write code avoids losing it later.
  2. Show the Developer tab (optional but useful). On Windows open File > Options > Customize Ribbon and tick Developer in the right-hand list. On a Mac open Excel > Preferences > Ribbon & Toolbar (or View > Ribbon depending on version) and tick Developer. The tab holds the Visual Basic, Macros and Record Macro buttons.
  3. Open the editor. Press Alt + F11 (Windows) or Option + F11 (Mac). You can also click Developer > Visual Basic. A separate window opens with a grey background and three main panes: Project Explorer top left, Properties below it, and the Code window on the right.
  4. Find your workbook in the Project Explorer. If the pane is hidden, use View > Project Explorer. Expand VBAProject (YourFileName.xlsm) and then Microsoft Excel Objects. You will see sheet entries such as Sheet1 (Sheet1) and ThisWorkbook.
  5. Create a standard module for general code. Right-click the workbook name in Project Explorer, choose Insert > Module. Code that should run on any sheet belongs here. Code placed in a sheet's own module is tied to that sheet and runs only in response to events on it.
  6. Let Excel write a macro for you. Back in Excel, click Developer > Record Macro, give it a name (no spaces, must not start with a number), choose This Workbook in the Store macro in list, and perform a few actions. Click Stop Recording, then return to the editor with Alt + F11 and expand Modules > Module1. You will see something like Sub Macro1() with your recorded steps inside. Recorded code is verbose, so expect several lines for one click.
  7. Write a simple procedure. In the empty module, type a routine between the Sub and End Sub lines. For example, Sub Greet(), then Range("A1").Value = "Hello", then End Sub. Every object reference needs a dot, spelling must be exact, and each block needs its closing line. The editor does not auto-close blocks for you.
  8. Use the toolbar to run and step through. Place the cursor inside a procedure and press F5 to run it, or use Run > Run Sub/UserForm. Press F8 to step one line at a time and watch each line highlight as it executes. F9 sets a breakpoint on the current line so execution pauses there.
  9. Test with values you can see. Put a value in A1 in a scratch workbook and run a procedure that changes it, so you confirm the code reached the right place. Read results with the Immediate window (View > Immediate Window, or Ctrl+G on Windows). Type a question mark followed by an expression, such as ?Range("A1").Value, and press Enter to print the answer.
  10. Save and return to Excel. Press Ctrl+S in the editor, then close it with Alt+Q or the window's close button. Run your macro from Excel with Developer > Macros or the shortcut you assigned during recording.

Common Mistakes

  • Saving as .xlsx after writing code. Excel warns you that macro-free workbooks cannot store macros; clicking yes deletes them.
  • Putting general routines in a sheet module. They work, but they appear in the wrong place and can fire unexpectedly on sheet events. Keep reusable code in a standard module.
  • Using spaces or starting a macro name with a digit. Both cause a compile error.
  • Forgetting that recorded code uses absolute references or selection, so a macro that worked on one range fails elsewhere. Replace selection-based lines with explicit range references.
  • Leaving the editor in break mode and then wondering why Excel will not respond. Press Run > Reset (the block-square icon) to stop execution.
  • Assuming code copied from a forum is safe. Only run macros you understand or have reviewed.

Tips

  • Add Option Explicit at the top of a module. It forces you to declare every variable with Dim, which catches typos at compile time instead of producing silently wrong results.
  • Indent consistently. Select code and use Tab or Shift+Tab to move whole blocks; the editor has no auto-format per line.
  • Use Debug > Compile VBAProject before running anything important. It surfaces syntax errors across the whole project rather than at one line.
  • Keep a blank test workbook beside your real one. Run new code there first, especially anything that deletes sheets, writes files or loops over many rows.
  • Variables declared with Dim lose their values when a procedure ends, so store data you need later in cells, names or module-level variables.
  • If a macro touches data, have it save a copy of the workbook before changes, or test on a copied file — VBA has no built-in undo.

Troubleshooting

If Alt + F11 does nothing, check whether the workbook is open in Excel for the web or a mobile app; the editor does not exist there. In a desktop copy, a locked-down Trust Center or an administrator policy may block VBA — look at File > Options > Trust Center > Trust Center Settings and ask IT if options are greyed out. On a Mac, confirm the Visual Basic Editor was included in the Office installer; if it is missing, reinstall Office from Microsoft's official download.

If the Project Explorer is empty or your workbook is missing from it, choose View > Project Explorer and make sure the workbook is actually open rather than closed after a crash. If code refuses to run, look for red-highlighted lines after compiling, confirm the module is not in a broken state, and press Run > Reset. Errors with a number and message, such as subscript out of range, usually mean a sheet name or range does not exist as typed — check spelling rather than assuming the workbook is broken.

If macros will not run because of a security warning, open the file from a location you trust and use the message bar to enable content once, rather than lowering macro security permanently. If the editor crashes or behaves oddly, save, close Excel fully, reopen, and repair Office from the Windows Control Panel > Programs list or the Microsoft support tool if problems continue.

Frequently Asked Questions

Can I open the VBA Editor in Excel for the web or on a phone?

No. The Visual Basic Editor is only in the desktop versions of Excel for Windows and Mac. Web and mobile apps can run macros in limited ways but cannot show or edit code.

Where should I put my code?

In a standard module for general routines, in a sheet module for code triggered by that sheet, and in ThisWorkbook for workbook-level events such as opening or closing.

Why did my macro disappear after saving?

You almost certainly saved as .xlsx. Save as .xlsm instead; once code is gone from a saved copy, it cannot be recovered from that file.

Do I need to enable the Developer tab?

No. Alt + F11 or Option + F11 opens the editor regardless. The tab only adds convenient buttons.

Can I undo a macro's changes?

No. Ctrl+Z does not reverse VBA actions. Keep a backup copy before running code that changes or deletes data.