How to Enter VBA Code in Excel
In this article
Entering VBA code in Excel lets you automate repetitive tasks, create custom functions, and build interactive forms — turning spreadsheets into powerful tools. You don’t need to be a programmer, but you must enable the Developer tab and use the built-in VBA editor. This guide walks you through the exact steps to write, insert, and run VBA code safely in Excel on Windows or macOS.
What You'll Need
- Microsoft Excel 2010 or later (Windows or macOS)
- A workbook saved as a macro-enabled file (.xlsm)
- Enabled Developer tab in Excel
- Basic understanding of what you want the code to do (e.g., auto-format cells, loop through data)
Step-by-Step
- Open Excel and create or open the workbook where you want to add VBA code. If you’re starting fresh, click File > New > Blank Workbook.
- Save the workbook as a macro-enabled file. Click File > Save As, choose a location, then in the Save as type dropdown, select Excel Macro-Enabled Workbook (*.xlsm). Click Save. This step is critical — Excel will block VBA code in .xlsx files.
- Enable the Developer tab if it’s not already visible. Go to File > Options (on macOS, go to Excel > Preferences > Ribbon), then select Customize Ribbon. In the right panel, check the box next to Developer, then click OK. Labels may vary slightly by region or Excel version.
- Open the VBA editor. Click the Developer tab, then click Visual Basic. Alternatively, press Alt + F11 on Windows or Option + Command + F11 on macOS. The VBA window will open.
- In the VBA editor, find the Project Explorer panel on the left (if missing, press Ctrl + R on Windows or Command + R on macOS). Right-click your workbook’s name (e.g., VBAProject (YourWorkbook.xlsm)), then select Insert > Module. This creates a blank code module.
- Type or paste your VBA code into the blank code window that appears. For example, to create a simple message box, enter:
Sub ShowMessage() MsgBox "Hello, this is VBA!" End Sub. Always start withSuband end withEnd Subfor macros. - Save your workbook again (Ctrl+S or Cmd+S) to preserve the code. Close the VBA editor by clicking the X in the top-right corner or pressing Alt + Q.
- Run the code. Return to Excel, click the Developer tab, then click Macros. Select the macro name (e.g., ShowMessage), then click Run. Alternatively, press Alt + F8, select the macro, and click Run.
Common Mistakes
- Trying to run VBA code in a .xlsx file — Excel blocks it silently. Always save as .xlsm.
- Typing code into the wrong window — ensure you’re in a Module, not ThisWorkbook or a Sheet module, unless you specifically need event-driven code.
- Forgetting to save after editing code — unsaved changes vanish when the VBA editor closes.
- Using incorrect syntax — misspelled commands like
MsgBosinstead ofMsgBoxcause runtime errors. - Running code on a protected worksheet without unprotecting it first — code may fail with an error about "object required" or "permission denied".
Tips
- Use comments liberally — start a line with an apostrophe (
') to explain what the code does. It helps you and others understand it later. - Test small pieces first — write a single line like
MsgBox "Test"and run it to confirm the editor is working. - Use the Immediate Window (Ctrl+G) to test expressions or debug variables during runtime.
- Always backup your workbook before adding complex code — VBA errors can corrupt data if not handled properly.
- Use named ranges or cell references instead of hardcoding A1:B10 — it makes your code reusable across different datasets.
- On macOS, some keyboard shortcuts differ — if Alt+F11 doesn’t work, use the Developer tab instead.
Troubleshooting
- The Developer tab is missing — Go to File > Options > Customize Ribbon and ensure Developer is checked. Restart Excel if needed.
- "Macro has been disabled" — Enable content when opening the file. A yellow security bar will appear below the ribbon — click Enable Content.
- Code won’t run — Check that you saved as .xlsm, that the macro name in the list matches your Sub name, and that you didn’t accidentally delete
SuborEnd Sub. - Runtime error 9: Subscript out of range — This usually means your code refers to a worksheet or range that doesn’t exist. Double-check sheet names in your code match the actual worksheet names exactly, including capitalization and spaces.
- VBA editor won’t open — Try restarting Excel. If the problem persists, repair your Office installation via Control Panel > Programs > Microsoft Office > Change > Quick Repair.
- Code runs but does nothing — Add
Debug.Printstatements to trace execution, or step through the code using F8 in the VBA editor.