Add Button in Excel to Run Macro

If you just need to add button in excel to run macro and move on, the boxed answer at the top is all you need. The rest of this page is for when you want to understand why it works in Excel, adapt it to a trickier version, or make it robust enough to hand to a colleague. We keep the opening short on purpose — the depth is here when you want it, not in your way when you don’t.

Exact answer

In Excel: Developer ▸ Insert ▸ Button (Form Control), drag out the rectangle on the sheet, and pick your macro from the Assign Macro dialog that opens. Excel for the web cannot create, run, or edit VBA macros. Open the workbook in the Excel desktop app to work with macros.

On this page8

VBA macro: Add a Button That Runs a Macro

Scroll code horizontally to see more
Sub AddMacroButton()
    Dim ws As Worksheet
    Dim btn As Button
    Set ws = ActiveSheet
    Set btn = ws.Buttons.Add(ws.Range("F2").Left, ws.Range("F2").Top, 130, 28)
    btn.OnAction = "SayHello"
    btn.Caption = "Run SayHello"
End Sub

Sub SayHello()
    MsgBox "The button ran this macro.", vbInformation
End Sub

Buttons.Add takes Left, Top, Width and Height in points, so anchoring to a cell's .Left/.Top drops the button neatly over F2. OnAction holds the macro NAME as text — a typo there fails silently at click time, not at compile time.

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

Turn on the Developer tab if it is not visible: File ▸ Options ▸ Customize Ribbon, tick Developer, click OK.

2

Choose Developer ▸ Insert and pick Button under Form Controls — the top-left icon, not the ActiveX section below it.

3

Drag out a rectangle where you want the button to sit; the Assign Macro dialog opens automatically.

4

Select the macro name and click OK.

5

Right-click the button and choose Edit Text to relabel it — never left-click, which runs the macro — then click any cell to leave edit mode and test it.

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

What this does

A button is the difference between a macro only its author can run and one the whole team can. Excel offers three ways to place one. The Form Control button is the right default: Developer ▸ Insert ▸ Button, drag a rectangle, and Excel immediately opens Assign Macro so you can pick the procedure. It is lightweight, survives being copied between workbooks, and works on Mac. The ActiveX command button in the same menu looks more configurable but is Windows-only and notorious for corrupting itself, so avoid it unless you need a property the Form Control lacks. The third option skips controls entirely: draw any shape via Insert ▸ Shapes, right-click it, and choose Assign Macro — you get a button that can look like anything, which is how most polished dashboards do it. Before the code does anything, check the environment it needs: desktop Excel, a macro-enabled workbook (.xlsm) and macros allowed for this file. If the workbook came by email or download, Office can block its macros until the file is trusted. For “add button in excel to run macro”, the reliable version is a short checking loop, not just the first command that appears to work. Run it on a deliberately small range first, watch how the affected cells change, and only then apply the same setup to the full sheet. 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. That is what makes a workflow that saves repeating the same clicks every week useful in real work: repeatable, auditable, and not dependent on memory or luck.

A worked example

Your workbook has a macro named RefreshReport and colleagues keep asking where to find it. Open the Developer tab (File ▸ Options ▸ Customize Ribbon ▸ tick Developer if it is missing), choose Insert ▸ Button under Form Controls, and drag a rectangle beside the data. The Assign Macro dialog opens; select RefreshReport and click OK. To relabel it, right-click the button and choose Edit Text — a left-click just runs the macro, so right-click is the instruction that works whether or not the control still happens to be selected. Now anyone can run the macro without ever opening the editor. To do the same in code rather than by hand, run Sub AddMacroButton() — it places a button over F2 and points its OnAction at SayHello, which is useful when a macro has to build its own interface across many sheets. A macro nobody can find is a macro nobody uses. Putting a labelled button on the sheet removes the Alt + F11 barrier entirely, which is what makes an automation shareable with people who will never open the editor. If the macro will be shared, add a comment at the top saying what it changes and which sheet it expects, and keep the workbook as .xlsm — saving it as .xlsx removes the code.

In Google Sheets

Google Sheets does not run VBA. A macro written for Excel has to be rewritten in Google Apps Script (Extensions ▸ Apps Script), and an .xlsm file converted to Sheets keeps its cells but drops the code. Keep this page bookmarked for the next time the same question comes up. Better still, repeat the steps once in your own workbook — doing it yourself is what turns a copied answer into something you remember. Treat “add button in excel to run macro” as code you are responsible for: know what it changes, keep a copy of the workbook before running it and keep the file in a macro-enabled format. In Google Sheets and Excel for the web this code does not run; those need a different mechanism.

Common mistakes

  • Picking the ActiveX command button instead of the Form Control. ActiveX controls are Windows-only, break on Mac, and are a well-known source of "Cannot insert object" corruption after the file has moved between machines.
  • Left-clicking the finished button to move or edit it, which just runs the macro. Right-click (or Ctrl + click on Mac) selects it for editing instead.
  • Assuming a button carries its macro with it. Copying a button into another workbook copies only the macro NAME held in OnAction; if no procedure of that name exists there, the click fails with "cannot be found".
  • Letting the button float over data. Right-click ▸ Format Control ▸ Properties ▸ "Don't move or size with cells" keeps it in place when rows are inserted or columns resized.

Frequently asked questions

How do I edit a button without triggering the macro?

Right-click it (Ctrl + click on Mac). That selects the control and opens the context menu with Edit Text, Assign Macro and Format Control, without running anything.

Can I use a picture or a shape instead of a grey button?

Yes, and most dashboards do. Insert ▸ Shapes or Insert ▸ Pictures, then right-click the object and choose Assign Macro. It behaves exactly like a Form Control button.

My button says the macro cannot be found — why?

OnAction stores the macro name as plain text, so renaming the Sub, deleting its module, or copying the button to a workbook without that procedure all break the link. Right-click ▸ Assign Macro and re-select it.

Can one button run several macros?

Indirectly. Point the button at a single wrapper Sub that calls the others in order — that also makes the sequence easy to change later without touching the button.