To run a VBA module in Excel, open the workbook that contains it, press Alt+F11 to open the Visual Basic Editor, then either press F5 with the cursor inside a macro that takes no arguments, or return to Excel and run it from the Macro dialog with Alt+F8. If the module holds more than one procedure, only the one your cursor sits in (or the one you pick in the dialog) runs. Everything else needs a few preconditions in place first, mainly macros enabled and code that does not trip over its own settings.

What You'll Need

  • Microsoft Excel on Windows or Mac (the VBA Editor is included; no separate install).
  • A workbook saved in a macro-enabled format (.xlsm) if the code must survive closing and reopening. A plain .xlsx strips VBA out when saved.
  • The Developer tab visible, or at least willingness to use keyboard shortcuts instead.
  • Permission: many workplaces block macros through Trust Center policy. If so, you cannot override that from inside a workbook.

Step-by-Step

  1. Make macros allowed for this file. If the workbook shows a yellow security bar on open, click the Enable Content button. If macros are disabled entirely, open File > Options > Trust Center > Trust Center Settings > Macro Settings and pick an option that allows macros with notification rather than one that disables them all. Exact wording of these labels can vary slightly by Excel version and by organisation policy.
  2. Open the Visual Basic Editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab's Visual Basic button). A separate grey window appears with a Project Explorer pane on the left listing your open workbooks.
  3. Find the module. In the Project Explorer, expand your workbook, then expand its Modules folder. Standard modules appear as Module1, Module2 and so on; code can also live in sheet objects (Sheet1) or in ThisWorkbook. Double-click the module so its code appears in the large white pane on the right.
  4. Check what is in the module. A module can hold several Sub procedures, Private helpers, and Function procedures. Only a Sub can be run directly from the Macro dialog. A Function runs only when called from other code, from a cell formula, or from the Immediate window.
  5. Compile early. With the module open, choose Debug > Compile VBAProject. This catches syntax errors before you run anything. Fix whatever it flags at the highlighted line.
  6. Run with F5. Click anywhere inside the Sub you want and press F5. The code runs immediately. If the Sub takes arguments (for example Sub Report(ByVal monthName As String)), F5 opens a small dialog asking for the values. If your cursor is outside any procedure, F5 prompts you with a list of macros to choose from.
  7. Or run from Excel itself. Switch back to the Excel window with Alt+F11 or Alt+Tab, press Alt+F8, select the macro name, and click Run. This is the everyday method for anyone who will use the workbook later, and it lists every public Sub across all open macro-enabled workbooks.
  8. Watch it happen. Many macros finish in a fraction of a second with no visible feedback. If you expect to see something and nothing changes, check that the code actually acts on the sheet you are looking at, for example by explicitly referencing a worksheet name inside the code.
  9. Save the workbook in .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. If a dialog warns that the file contains VBA, accept it; that is exactly what you want.
  10. Optional: attach the macro to a button. On the Developer tab, insert a Button form control, draw it on the sheet, then choose the macro in the Assign Macro dialog. Users then click the button instead of hunting through Alt+F8.

Common Mistakes

  • Saving as .xlsx. Excel warns once, but people click through it. On reopening, all code is gone. Always confirm the title bar shows the macro-enabled format.
  • Running a Function with F5. F5 on a Function does nothing useful; there is no argument to supply. Call it from a Sub, use the Immediate window with a question mark, or reference it in a worksheet cell.
  • Cursor in the wrong procedure. In a module with three Subs, F5 runs the one containing the cursor, not the first one. People edit Sub A, click in Sub B, and blame the code.
  • Believing Alt+F8 is broken. The dialog only shows public Subs that take no required arguments in open macro-enabled workbooks. Private Subs and Subs with mandatory parameters simply do not appear.
  • Running on the wrong sheet. Code that says Range("A1").Value acts on whichever sheet is active. Switch sheets and the macro edits a different place. Explicit Worksheets("Data").Range(...) avoids this entirely.
  • Forgetting that macros cannot be undone. Ctrl+Z does nothing after VBA edits cells. Test on a copy of the workbook, not on live data.

Tips

  • Add a keyboard shortcut when you assign a macro through the Macro dialog's Options button. Ctrl plus a letter is quick, but avoid Ctrl+letters Excel already uses.
  • Set a breakpoint by clicking the grey margin beside a line, then run. Execution pauses and you can step through with F8. This is far faster than adding message boxes everywhere.
  • Use Option Explicit at the top of the module. It forces every variable to be declared, which kills a whole class of silent bugs.
  • If the code errors, choose Debug when prompted rather than End. The highlighted line tells you exactly where it stopped.
  • Sign your own macros with a self-signed certificate if you run them often in a locked-down environment; the certificate then keeps them trusted.
  • Give Sub procedures meaningful names, not Macro1, so the Alt+F8 list stays readable.

Troubleshooting

The macro is missing from Alt+F8. You are probably in a workbook that is not macro-enabled, macros are disabled, or the procedure is Private or has required arguments. Reopen with content enabled and confirm the format.

The code runs but nothing happens. Check for Application.ScreenUpdating = False lines that never get reset to True, or code that targets a different sheet than the one you are watching. Step through with F8 to confirm.

A runtime error appears. Read the number and message; nine times out of ten it is a name that does not exist, a closed workbook, or an empty cell being treated as a number. Press Debug and inspect the offending line.

A "macros have been disabled" bar will not go away. This is Trust Center policy or a file from an untrusted location. Right-click the file, check Properties for an Unblock option, or move it into a trusted folder your administrator controls. Do not try to defeat a policy you do not own.

The macro was there and now it is not. A save in .xlsx format has removed it. Restore from a backup or from the previous version; it cannot be recovered from the stripped file.

Frequently Asked Questions

What is the fastest way to run a macro?

If you are editing code, Alt+F11 then F5 with the cursor in the procedure. If you are using a finished workbook, Alt+F8, select the name, press Enter.

Can I run a module without opening the VBA Editor?

Yes. Assign the macro to a button, a shape, or a keyboard shortcut, or run it from Alt+F8. The module is only needed when you want to read or change the code.

Why does Alt+F8 not list my macro?

It must be a public Sub with no required arguments, in an open macro-enabled workbook. Private procedures, Functions and Subs with mandatory parameters are hidden from that list.

Does running a VBA module work on Mac?

Yes, the Visual Basic Editor and Alt+F8 exist on Excel for Mac, though some Windows-only features such as certain API calls will not compile. Basic cell-editing code runs the same.

Can a macro run automatically when the workbook opens?

Yes. A Sub named Workbook_Open placed in the ThisWorkbook object runs on open, provided the user enables macros. That is a common way to deliver a module without asking people to press anything.

What happens if I press Ctrl+Break during a macro?

It interrupts execution and drops you into the editor at the current line. Use this when a loop is running far longer than expected.