How to Execute VBA Code in Excel

There are two ways to “execute vba code in excel”: the quick way you copy and the durable way you understand. This page gives you both. The exact Excel answer is above; below, we build the small mental model that makes the fix stick, so the next variation of the same problem solves itself.

Exact answer

In Excel: Press Alt + F8, select the macro by name, and click Run — or press F5 with the cursor inside the Sub in the VBA editor.

VBA macro: Run a Macro in Excel

Sub RunMeFromTheMacroDialog()
    Dim ws As Worksheet
    Dim started As Double
    Set ws = ActiveSheet
    started = Timer
    ws.Range("A1").Value = "Ran at " & Format(Now, "yyyy-mm-dd hh:mm:ss")
    MsgBox "Macro completed in " & Format(Timer - started, "0.000") & " seconds.", vbInformation
End Sub

Timer returns seconds since midnight, so subtracting a start value times the run. Stamping the sheet as well as showing a dialog proves the macro really executed rather than merely appearing to.

How to run this macro

  1. Press Alt + F11 to open the VBA editor.
  2. Insert > Module.
  3. Paste the code above.
  4. Press F5, or close the editor and run it from Developer > Macros.
  5. Save the file as .xlsm so the macro is kept.
Annotated stepsExcel
1

Open the workbook and click Enable Content if the security bar appears — macros will not run while the file is blocked.

2

Press Alt + F8 to open the Macros dialog.

3

Select the macro by name; the list shows every public Sub across all open workbooks.

4

Click Run, or click Options first to attach a Ctrl + Shift + letter shortcut.

5

To run while editing instead, press Alt + F11, click inside the Sub, and press F5.

Ctrl+CthenCtrl+Shift+V+Cthen+Ctrl+VPaste values · WindowsMac

What this does

There are four practical ways to start a macro, and which one you want depends on who is doing the starting. Alt + F8 opens the Macros dialog listing every public Sub in every open workbook; select and click Run. That is the general-purpose route. F5 inside the editor runs whichever Sub the cursor currently sits in, which is what you use while building and testing. A keyboard shortcut can be attached under Alt + F8 ▸ Options — pick a letter with Ctrl + Shift rather than plain Ctrl, since plain Ctrl + letter combinations overwrite Excel's own shortcuts for that session. Finally, a button or shape with Assign Macro puts it in front of colleagues who will never learn any of the above. There is also automatic execution — event procedures in ThisWorkbook such as Workbook_Open run without anyone asking, which is powerful and worth being deliberate about. Most people learn this as a sequence of clicks and forget it by next week; learning it as a pattern instead is what lets you apply it to the next, slightly different version of the problem without starting from scratch. That is the difference this page is trying to make. The difference between a quick fix and a sheet you can trust is the extra minute you spend validating “execute vba code in excel”. Start on a copy or a tiny sample, keep the affected cells visible, and compare the result with the tool above before you touch the real workbook. When this is a ribbon command, the selection matters more than the button: confirm the range, apply the command, then spot-check the output before saving. The point is a workflow that saves repeating the same clicks every week, but the practical win is that someone else can open the file and understand what happened without asking you.

A worked example

A workbook arrives with a macro called RefreshReport. Click Enable Content on the yellow bar, then press Alt + F8. The dialog lists RefreshReport; select it and click Run. To time it or watch it work, press Alt + F11 instead, click anywhere inside the Sub, and press F5 — that runs the same code but leaves you in the editor where Debug.Print output and any error dialog are visible. Paste Sub RunMeFromTheMacroDialog() into a module and run it both ways: A1 gets a timestamp and the dialog reports the elapsed time, so you can tell at a glance that it ran and how long it took. If the macro does not appear in the Alt + F8 list at all, it is Private, it takes arguments, or it lives in a sheet object rather than a standard module. Knowing all four entry points matters because they fail differently: F5 shows you the error, a button hides it, and a keyboard shortcut can collide with Excel's own. Picking the right one for the audience is most of what makes an automation usable by someone other than its author. One habit worth forming early: name the cells that hold your inputs, so the formula reads in plain language instead of a string of cell addresses. A reviewer — or you in three months — can then follow the logic without decoding what B7 and D2 were supposed to mean, which is most of what makes a sheet maintainable.

In Google Sheets

Google Sheets handles this almost identically to Excel. The formula syntax above is the same, and the menu lives under a slightly different label rather than a ribbon tab. Use the platform toggle at the top of the page to switch every keyboard shortcut between Windows and Mac, and expect at most cosmetic differences in naming. Keep this page bookmarked for the next time the same question comes up. Better still, rebuild the example once in your own sheet — doing it yourself, with the tool above to check against, is what turns a copied formula into a technique you own. Here is the takeaway for “execute vba code in excel”: copy the answer if you are busy, but if you have a spare few minutes, rebuild the example in Excel yourself with the tool above open beside it. That single pass — type it, run it, watch the result move when you change an input — is what turns a formula you found into a technique you trust. Keep your inputs labelled and referenced, never hard-coded, and the same sheet stays correct and auditable as it grows. Done that way, you will not need to look this up again, and you will be the person others ask.

Common mistakes

  • Running a macro that modifies data without a backup. VBA writes bypass Excel's undo stack, so Ctrl + Z cannot recover anything a macro changed — test on a copy first, every time.
  • Assigning a plain Ctrl + letter shortcut. It silently overrides the built-in shortcut for as long as the workbook is open, so Ctrl + C or Ctrl + S can stop doing what everyone expects. Ctrl + Shift + letter is the safe range.
  • Expecting a macro from workbook A to find data in workbook B. Unqualified references like Range("A1") resolve against whatever is active at the moment the line executes, so which sheet you are looking at when you press Run changes the result.
  • Concluding a macro is missing because Alt + F8 does not list it. Private Subs, Subs that take arguments, and code inside sheet objects are all hidden from that dialog while still being perfectly runnable with F5.

Frequently asked questions

What is the keyboard shortcut to run a macro?

Alt + F8 opens the Macros dialog on the sheet; F5 runs the current Sub inside the VBA editor. You can assign a personal shortcut per macro under Alt + F8 ▸ Options.

How do I stop a macro that is taking too long?

Press Esc, or Ctrl + Break. The editor then offers Continue, End or Debug — Debug stops on the current line so you can see where it was stuck.

Can a macro run automatically when the workbook opens?

Yes. A Private Sub Workbook_Open() in the ThisWorkbook module runs on open, and Worksheet_Change in a sheet module runs whenever a cell there is edited. Both still require macros to be enabled.

Why does nothing happen when I click Run?

Most often macros are still disabled, or the Sub really did run but acted on a different sheet than the one you are looking at. Add a MsgBox as the last line to confirm it reached the end.

Other ways people ask this

On the way here you may have searched this as “excel vba execute” and “execute vba in excel” — it is all the same task, and this page is the single, complete answer to it.

Why do people search for this in so many different ways?

Because the same task has many names. “excel vba execute”, “execute vba in excel” all point at the one operation explained on this page, which is why they all lead here.