How To Execute VBA Code In Excel | Write, Store, And Run

Running VBA code in Excel requires the Developer tab, the Visual Basic Editor to write or paste it, and a macro-enabled workbook to store the macro.

Learning how to execute VBA code in Excel opens up automation possibilities that manual spreadsheet work can’t match. Whether you wrote the code yourself, copied it from a tutorial, or recorded a macro, the execution path is the same: get the code into the right location, then run it from the correct dialog or trigger. Below is the full workflow, from enabling the tools you need to avoiding the mistakes that stop your code cold.

Executing VBA Code In Excel: The Tools You Need

The Developer tab is the command center for VBA work. Excel hides it by default, so the first step is turning it on. Go to File > Options > Customize Ribbon, then check Developer in the right-hand list and click OK. The Developer tab now appears in your ribbon, giving you access to the Visual Basic Editor, the Macro dialog, and form controls.

Open The Visual Basic Editor (VBE)

The Visual Basic Editor is where your code lives. Press Alt+F11 to open it directly, or click Developer > Visual Basic from the ribbon. The VBE shows a Project Explorer on the left (if it’s hidden, press Ctrl+R) and a code window on the right. Every open workbook appears as a project tree under its name.

Insert A Module And Paste Your Code

Standard macros live inside modules. In the Project Explorer, right-click the workbook’s name and choose Insert > Module. A blank code window opens. Paste or type your VBA code there. Each procedure starts with Sub and ends with End Sub; the first line after Sub is the macro name that will appear in the run dialog.

The Macro Dialog Is The Safest Way To Run Your Code

The Macro dialog gives you a clear list of every available procedure and is the method Microsoft documents as the standard run path. Close the VBE and return to your worksheet. Click Developer > Macros (or press Alt+F8). Select your macro by name from the list, then click Run. The code executes immediately against the active workbook. This method works regardless of cursor position or window focus, which makes it the most reliable choice for beginners. Microsoft’s official VBA getting-started guide uses this exact dialog for running recorded macros.Microsoft’s VBA getting-started documentation covers the full record-and-run workflow.

Execution Method How To Access It Best Use Case
Macro Dialog Developer > Macros or Alt+F8 Safest path for beginners; lists all available macros
F5 Inside VBE Place cursor inside the procedure and press F5 Fast testing while editing code
Keyboard Shortcut Assign in Macro dialog (Options button) Daily-use macros you run frequently
Shape Or Button Insert a shape, right-click > Assign Macro One-click launch for non-technical users
Form Control Button Developer > Insert > Button (Form Control) Spreadsheet-integrated execution with visible label
Quick Access Toolbar Excel Options > Quick Access Toolbar > Macros Always-visible button regardless of ribbon state
Event Procedure Code placed in worksheet or workbook object Automatic execution on open, change, or other triggers

Save Your Workbook As A Macro-Enabled File

Standard Excel workbooks use the .xlsx extension, which cannot store VBA code. After adding your macro, save the file as Excel Macro-Enabled Workbook (.xlsm) using File > Save As and picking Excel Macro-Enabled Workbook from the file type dropdown. If you try to save a file with macros in the .xlsx format, Excel shows a compatibility warning that the code will be discarded. Until you save as .xlsm, closing and reopening the file erases your VBA work.

Can You Run VBA From A Keyboard Shortcut Or Button?

Yes, and both options save time once the macro is stable. To assign a keyboard shortcut, open the Macro dialog (Alt+F8), select your macro, and click Options. Enter a letter in the Ctrl+ field — Ctrl+Shift+ combinations avoid overriding built-in Excel shortcuts. To use a button, insert a shape via Insert > Shapes, right-click it, choose Assign Macro, and select your procedure. Clicking the shape then runs the code. For event-driven macros placed in worksheet or workbook objects, the code runs automatically when the event occurs — no manual launch needed at all.

What Common Mistakes Stop VBA Code From Running?

Most execution failures come from a short list of avoidable errors. The code may be in the wrong location, the file may not be macro-enabled, or Excel’s security settings may block it. Each problem has a straightforward fix once you know what to check.

Problem Likely Cause Fix
Macro button is grayed out Macros are disabled in Trust Center File > Options > Trust Center > Trust Center Settings > Macro Settings > Enable all macros (temporary)
“Cannot run the macro” error Security settings or unsigned code Enable all macros or digitally sign the macro
Code is gone after reopening the file Saved as .xlsx instead of .xlsm Save as Excel Macro-Enabled Workbook (.xlsm)
Nothing happens when I run it Wrong procedure selected in Macro dialog Open Macro dialog and verify the correct macro name is highlighted
Code is pasted into a worksheet cell Pasted into the grid instead of a Module Cut the code, open VBE, insert a Module, paste there
Event code doesn’t fire automatically Placed in a standard Module instead of the object Move the event procedure to the Worksheet or Workbook object in Project Explorer
“Sub or Function not defined” Typo, missing procedure, or missing reference Double-check spelling; ensure the referenced library is enabled under Tools > References

The Complete Workflow To Execute VBA Code

Here is the full sequence in one place — run through these steps and your code will execute every time:

  1. Enable the Developer tab under File > Options > Customize Ribbon.
  2. Press Alt+F11 to open the Visual Basic Editor.
  3. In Project Explorer, right-click your workbook and choose Insert > Module.
  4. Paste your VBA code into the blank module window.
  5. Close the VBE and press Alt+F8 to open the Macro dialog.
  6. Select your macro by name and click Run.
  7. Save the workbook as Excel Macro-Enabled Workbook (.xlsm) to preserve the code.

Once the code runs correctly, consider assigning it to a keyboard shortcut or a button for one-click access. And if the macro is meant to run automatically when you open the workbook or change a cell, move the code into the event procedure window of the relevant object rather than a standard module. Following this sequence sidesteps the most common pitfalls and gets your VBA working on the first try.

References & Sources