Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Enter VBA Code in Excel

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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 with Sub and end with End Sub for macros.
  7. 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.
  8. 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 MsgBos instead of MsgBox cause 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 Sub or End 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.Print statements to trace execution, or step through the code using F8 in the VBA editor.

Frequently Asked Questions

Can I use VBA code in Excel Online?

No. Excel Online does not support VBA. You must use the desktop version of Excel (Windows or macOS) to write or run macros.

Is VBA safe to use?

Yes, if the code comes from a trusted source. Excel warns you when opening macro-enabled files. Never enable macros from untrusted senders — malicious code can be embedded.

Can I edit VBA code without reopening the editor?

Not directly. You must open the VBA editor (Alt+F11) to modify code. Changes made in Excel cells won’t affect the code.

Do I need to know programming to use VBA?

No. Many users copy and adapt code from templates or forums. Understanding basic logic — like loops and conditions — helps, but you can start with simple recorded macros.

Why does my code disappear after closing Excel?

You saved the file as .xlsx instead of .xlsm. Only .xlsm files preserve VBA modules. Always confirm the file type before closing.

Can I run VBA code on a Mac?

Yes. Excel for Mac supports VBA, but some object model features differ slightly. Always test your code on the version you intend to use.