Smart Systems for Home

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

Enter VBA Code in Excel: Step-by-Step Guide

· 7 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

You enter VBA code in Excel through the Visual Basic Editor, which is built into every Windows and Mac copy of Excel. The fastest route is to press Alt+F11 (Windows) or Option+F11 (Mac), then use Insert > Module to create a blank code container and paste or type your code into it. You can also place code behind a specific sheet or workbook by double-clicking it in the Project pane instead of inserting a module. Either way you must save the file as an .xlsm workbook, or Excel discards the code when you close it — that single step is the most common reason working code seems to vanish.

What You'll Need

  • A desktop copy of Microsoft Excel for Windows or Mac (Excel on the web and the mobile apps do not include the Visual Basic Editor).
  • The Developer tab enabled, or enough comfort with the keyboard shortcut to open the editor.
  • The VBA code you want to enter, ideally already tested or copied from a trusted source — never run code you do not understand from an unknown sender.
  • The workbook open, and the ability to save it in the macro-enabled format (.xlsm).

Step-by-Step

  1. Enable the Developer tab (recommended). Go to File > Options > Customize Ribbon, then tick the Developer checkbox in the right-hand list and click OK. On a Mac, open Excel > Preferences > Ribbon & Toolbar and tick Developer. Labels can differ slightly by version or region, and this step is optional if you prefer the keyboard shortcut.
  2. Open the Visual Basic Editor. You can click Developer > Visual Basic on the ribbon, or press Alt+F11 on Windows or Option+F11 on a Mac. A second window opens with a grey background and its own menus — that is the editor, not Excel.
  3. Choose where the code should live. Look at the Project pane on the left. It usually shows something like VBAProject plus a folder tree with your workbook name, a Microsoft Excel Objects folder, and folders for Modules and, sometimes, Forms. If the pane is missing, use View > Project Explorer to show it.
  4. Insert a standard module. Click your workbook in the Project tree so the code is attached to the right file, then choose Insert > Module. A blank white code window appears in the middle. Standard modules are the right home for ordinary routines that work on data, such as a macro that formats a table or loops through cells.
  5. Place code behind a sheet if needed. If your code is an event handler — something that must run automatically when a cell changes or the sheet is activated — double-click Sheet1 (or the relevant sheet name) under Microsoft Excel Objects and paste the code there instead. Event code pasted into a normal module simply will not fire.
  6. Type or paste the code. Type directly into the blank window, or press Ctrl+V to paste. Use Ctrl+Tab to indent a line and Shift+Tab to reverse it; keep indentation tidy so blocks are readable. If you paste, watch the first line — a Sub statement should appear once and end with End Sub, and nothing should sit above it except comments starting with an apostrophe.
  7. Let the editor catch basic errors. A line shown in red means a syntax error — an unbalanced quote, a missing Then after If, or text placed outside a Sub. Correct it before running. The editor also auto-capitalises keywords and reformats spacing, which is a useful sign the line parsed.
  8. Run the code. Place the cursor anywhere inside the routine and press F5, or use Run > Run Sub/UserForm. You can also close the editor and run it from Excel via Developer > Macros, selecting the name and clicking Run. If a dialog box appears asking which macro to run, your cursor was outside a routine.
  9. Save in a macro-enabled format. Back in Excel choose File > Save As and pick Excel Macro-Enabled Workbook (.xlsm). A plain .xlsx file cannot hold macros; Excel will warn you that they will be lost if you save in that format. For a one-off test this is fine, but for anything you want to keep, choose .xlsm.
  10. Reopen and handle the security prompt. Close and reopen the file. Excel usually shows a yellow bar saying macros are disabled. Click Enable Content to let the code run. Files downloaded from the internet may be blocked entirely until you right-click the file in File Explorer, choose Properties, and tick Unblock — only do this for files you trust.

Common Mistakes

  • Saving as .xlsx. The code disappears on close. Always use .xlsm.
  • Pasting a whole recorded macro on top of existing code. You can end up with two End Sub lines or a Sub nested inside another. Paste below the last routine, and check the count of Sub and End Sub lines matches.
  • Choosing the wrong container. A macro that is meant to auto-run belongs in the sheet or ThisWorkbook, not a module. Ordinary helper routines belong in a module, not a sheet.
  • Leaving the code in the wrong workbook. If the Project pane shows several open files, it is easy to insert a module in the wrong one. Click the intended workbook first, then Insert > Module.
  • Forgetting to reference an object. Unqualified range references run on the active sheet, which may not be the sheet you expect. Qualify them with the worksheet or workbook name.
  • Ignoring the security prompt. Code that is pasted correctly still does nothing while macros remain disabled.

Tips

  • Use the Immediate window (View > Immediate Window, or Ctrl+G) to test a single line instantly by prefixing it with a question mark, for example ?Range("A1").Value.
  • Turn on Option Explicit at the top of each module (Tools > Options has a setting to require variable declaration automatically) so undeclared variables are flagged rather than silently accepted.
  • Take a copy of the workbook before running unfamiliar code — macro errors can change data irreversibly, and undo does not always reverse a macro's effect.
  • Use comments liberally, starting each with an apostrophe, so a routine is understandable months later.
  • Keep one routine per logical task and give routines descriptive names rather than Macro1, Macro2.
  • When you reuse code across workbooks, save it as an .xlam add-in instead of copying it into every file.

Troubleshooting

The Developer tab is missing. Re-enable it via File > Options > Customize Ribbon, or just use Alt+F11. On a Mac, check Excel > Preferences > Ribbon & Toolbar.

Alt+F11 does nothing. On some laptops the function keys require the Fn key, so try Fn+Alt+F11. If the shortcut is still unresponsive, check whether another program is intercepting it, and use the ribbon button instead.

The code runs but nothing happens. Confirm the sheet it targets is the active sheet or that references are fully qualified, and check that entries in a Worksheet_Change or similar event routine are actually in the sheet's code module.

A compile error appears. Read the highlighted line, fix the reported syntax problem, then use Debug > Compile VBAProject to check the whole module at once. A common culprit is a reserved word used as a variable name.

The macro list is empty. Macros that take arguments, or routines declared Private, do not appear in the macro dialog. Make sure your routine is a plain Sub with no parameters and is not marked Private.

Excel blocks the macros or the file. Enable content in the yellow bar for files you trust; for downloaded files, unblock them in file Properties. Trusted locations in the Trust Center can be configured if you work with the same folder repeatedly.

You need to undo a change made by a macro. Close without saving if you have not saved since running it; otherwise restore from a backup copy. This is why testing on a copy matters.

Frequently Asked Questions

Can I enter VBA code in Excel on the web or on a phone?

No. The Visual Basic Editor is only in the desktop versions for Windows and Mac. Web and mobile versions can run existing macros in some situations but cannot create or edit VBA code.

Do I need to enable the Developer tab?

No, it is a convenience. Alt+F11 on Windows or Option+F11 on a Mac opens the editor without it.

Why does my code disappear after I close the workbook?

The file was almost certainly saved as .xlsx, which cannot store macros. Save as .xlsm and the code will persist.

What is the difference between a module and a sheet module?

A standard module holds routines you run on demand. A sheet or ThisWorkbook module holds event code that fires automatically when something happens in that sheet or workbook.

Is it safe to run VBA code from the internet?

Only if you trust the source and understand what the code does. Macros can read, modify and delete files. Open the editor and read the code before enabling it, and test on a copy of your workbook.

Can I edit a macro I recorded?

Yes. Recorded macros are ordinary VBA. Open the editor, find the routine in a module, and edit it directly — often shortening the many Select and Activate lines a recording produces.