Creating a new Excel VBA application means opening the Visual Basic Editor (VBE) and inserting a module or user form to hold your code. The quickest path: press Alt+F11 in Excel, then choose Insert > Module. That gives you a blank code window where you can write macros. This guide walks through the setup, the first runnable macro, and the common gotchas.

What You'll Need

  • Excel for Windows or Mac (Microsoft 365 or a recent perpetual version). The steps below use the Windows ribbon; Mac labels differ slightly.
  • Macro security set to allow macros in the workbook you are using. You can change this under File > Options > Trust Center > Trust Center Settings > Macro Settings (Windows) or Excel > Preferences > Security (Mac).
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the code.

Step-by-Step

  1. Open the workbook where the application will live. If you plan to keep the code, save it now as Excel Macro-Enabled Workbook (*.xlsm). A standard .xlsx will strip macros on save.
  2. Open the Visual Basic Editor. Press Alt+F11 (Windows) or Fn+Alt+F11 on some Mac keyboards. You can also use the Developer tab > Visual Basic. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list.
  3. Confirm the correct workbook is selected. In the VBE Project Explorer (usually top-left), you will see a tree such as VBAProject (YourFile.xlsm). If the panel is closed, open View > Project Explorer.
  4. Insert a standard module. With your workbook highlighted, choose Insert > Module. A blank code window opens. This is where general-purpose macros go. For buttons and forms, you would instead use Insert > UserForm, but start with a module.
  5. Write a minimal macro. Type the following in the code window:
    Sub HelloWorld()
        MsgBox "Hello from VBA"
    End Sub
    The Sub line starts the routine and End Sub closes it. Indentation is optional but helps readability.
  6. Run it. Place the cursor inside the subroutine and press F5, or use Run > Run Sub/UserForm. A message box should appear. Click OK.
  7. Save the workbook. Press Ctrl+S. If Excel prompts about macros, choose Yes to keep the .xlsm format. If you see a security warning on reopening, click Enable Content only if you trust the file.
  8. Optionally attach the macro to a button. On the sheet, go to Developer > Insert > Button (Form Control), draw the button, and assign the HelloWorld macro in the dialog that appears. This makes the application usable without the VBE.

Common Mistakes

  • Saving as .xlsx. The code disappears because the format does not support macros. Always use .xlsm.
  • Writing code in the wrong place. Code in a sheet module (e.g., Sheet1) only runs on that sheet's events. General macros belong in a standard module.
  • Forgetting End Sub. A missing End Sub causes a compile error and the macro will not run.
  • Macro security blocking everything. If the Run menu is greyed out or you get a security prompt, check Trust Center settings. Do not disable all security permanently; use a trusted location instead.
  • Editing while the macro is running. If Excel appears frozen, a running macro may be waiting on input. Press Esc or Ctrl+Break to interrupt.

Tips

  • Use Option Explicit at the top of each module (Tools > Options > Editor > Require Variable Declaration) to catch typos in variable names.
  • Press F8 to step through code line by line; hover over variables to see their current values.
  • Keep one module per purpose (e.g., one for import routines, one for formatting) so the project stays navigable.
  • Back up the .xlsm file before major edits. VBA has no built-in undo for code changes.
  • If you share the workbook, note that macros may be blocked by email gateways or by recipients' Trust Center settings.

Troubleshooting

If nothing happens when you press F5, click inside the subroutine first; F5 runs the routine containing the cursor. If you get a "Macros are disabled" bar, click Enable Content or adjust Trust Center. If the Developer tab is missing, enable it via ribbon customization as described above. If the code window is blank, confirm you inserted a module rather than opening an existing sheet's code pane. If Excel crashes on run, save your work, close and reopen Excel, and test the macro in a new blank workbook to isolate whether the issue is the file or the code. For persistent compile errors, use Debug > Compile VBAProject to jump to the offending line.

Frequently Asked Questions

Do I need the Developer tab to use VBA?

No. Alt+F11 opens the editor directly. The Developer tab is a convenience for buttons and macro settings.

Can I run VBA in Excel for the web?

No. Excel for the web does not run VBA macros. You need the desktop app.

Why does my macro disappear after saving?

The file was saved as .xlsx, which cannot store macros. Re-save as .xlsm.

Is VBA safe to use?

VBA can run any code the author writes, so only enable macros in files you trust. Keep macro security at the default and use trusted locations for your own workbooks.

What is the difference between a module and a UserForm?

A module holds plain subroutines and functions. A UserForm is a custom dialog window with controls; it still needs code behind it, usually in the form's own module.

Can I use VBA on a Mac?

Yes, Excel for Mac supports VBA, but some Windows-specific features (certain ActiveX controls, some file dialog options) are unavailable. Test on the target platform.