To attach a macro to a button in Excel for Microsoft 365 on Windows, open the Developer tab, choose Insert > Button (Form Control), draw the button on the worksheet, select the macro in the Assign Macro dialog, and click OK. If the Developer tab is hidden, turn it on through File > Options > Customize Ribbon.
What a Button-Assigned Macro Does
A button provides a visible, clickable way to run a VBA macro. Instead of opening the macro list or using a keyboard shortcut, a workbook user clicks the button and Excel runs the macro assigned to it. This is useful for repeatable actions such as clearing an input form, preparing a report, or applying a consistent layout.
The button does not contain the macro itself. The macro is stored in the workbook’s VBA project, and the button stores a reference to the macro Excel should run. This distinction matters when copying a button, renaming or moving a macro, or sharing the workbook: the button may remain visible even when its assigned macro is unavailable.
Use a Form Control button for a straightforward worksheet action. ActiveX command buttons are a different control type with extra event-based configuration; they are generally unnecessary for a basic “click to run a macro” task.
Show the Developer Tab
The Developer tab is hidden by default in many Excel installations. In Excel for Microsoft 365 on Windows, enable it as follows:
- Choose File > Options.
- Select Customize Ribbon.
- Under Main Tabs on the right, select the Developer check box.
- Click OK. Developer should now appear in the ribbon.
This changes the ribbon display, not the workbook’s contents. On Excel for Mac, enable the tab in Excel > Settings (or Preferences, depending on the release) > Ribbon & Toolbar, then select Developer. Excel for the web can display and edit many workbook features, but it cannot create, edit, or run VBA macros; use the Excel desktop app for this task.
Insert and Assign a Form Control Button
First, make sure the macro is available in the workbook. Then use these steps in Excel for Windows:
- Open the Developer tab and select Insert.
- Under Form Controls, choose Button. Avoid the ActiveX Controls section unless you specifically need an ActiveX control.
- Click and drag on the worksheet to draw the button. Excel opens the Assign Macro dialog.
- Select the macro you want to run. If it is not listed, check that the macro is in an open workbook and is an eligible public procedure.
- Click OK, then click a blank worksheet cell to finish placing the control.
- Click the button once to run the assigned macro.
If you already drew the button but dismissed the dialog, right-click the Form Control and choose Assign Macro. Pick the correct procedure and confirm. To change the visible wording, right-click the button, choose Edit Text, and type a concise label such as “Refresh Report.” Click a cell when you have finished editing the text.
Make Sure the Macro Is Eligible
A typical button macro is a Public Sub procedure with no required arguments, stored in a standard VBA module. For example, a procedure named RefreshReport can be selected in the Assign Macro dialog if it meets those conditions. A procedure that requires inputs cannot be run directly from an ordinary Form Control button without adding code or using another design to collect those inputs.
To add a basic macro, open the Visual Basic Editor with Alt+F11 on Windows, choose Insert > Module, and place the procedure in the module. The keyboard shortcut on Mac is usually Option+F11; depending on the keyboard, you may also need Fn. A simple, safe example that selects cell A1 on the active sheet is:
Public Sub GoToA1()
' Move the active cell to A1 on the current sheet.
Application.Goto Reference:=ActiveSheet.Range("A1")
End Sub
Assigning a macro does not make it safe or correct. Review what a procedure changes before running it, particularly if it deletes data, overwrites formulas, changes files, or interacts with external systems. Save a copy of the workbook before running a macro: VBA actions cannot be undone with Ctrl+Z.
Save, Share, and Protect the Workbook
A workbook that contains VBA should normally be saved as an Excel Macro-Enabled Workbook (*.xlsm). Choose File > Save As and select that file type. Saving as a standard .xlsx workbook removes the VBA project, so its macros will not be preserved. A button may still appear in a workbook after macro content is removed, but it cannot run the missing procedure.
When another person opens the file, Excel’s macro security settings may block macros or show a security notification. Users should enable macros only when they trust the file’s source and understand what the code does. Do not tell recipients to bypass security warnings indiscriminately; organizations may also apply policies that prevent users from enabling VBA.
Check the button and macro after moving the file or sharing it. A macro assigned by name can become unavailable if it is renamed, deleted, or moved to a workbook that is not open. If a button is intended to operate on a particular sheet or workbook, the code should refer to that target deliberately rather than relying on whichever sheet happens to be active.
Common Problems and Alternatives
If the button does nothing or Excel displays an error, work through these checks:
- The macro is missing from the dialog: Confirm that it is in an open workbook, stored in a standard module, declared Public, and has no required arguments.
- Excel blocks the macro: Check the security notification and the file’s source. Follow your organization’s security policy rather than lowering macro protection broadly.
- The button is hard to click or edit: Right-click it to access Assign Macro or Edit Text. If a cell is selected instead, try right-clicking the control’s border.
- The button runs the wrong action: Right-click it, choose Assign Macro, and verify the selected procedure and workbook name.
- The file was saved as .xlsx: Save a macro-enabled .xlsm copy and confirm that the VBA project is still present.
On a worksheet, a Form Control button is usually the simplest option. A shape can also be assigned a macro: insert or select a shape, right-click it, choose Assign Macro, and select the procedure. A shape is easier to style, while the Form Control button is clearly labeled as an interactive control. For automation that must run when a cell changes or a workbook opens, a worksheet or workbook event procedure may be more suitable than a button; events run automatically under their specified conditions, so they require extra care and testing.
Frequently Asked Questions
Can I attach more than one macro to a button?
A Form Control button is assigned one macro at a time. To run multiple actions from one click, create a separate procedure that calls the desired procedures in sequence, then assign that main procedure to the button.
Can I assign a macro to a shape instead?
Yes. Right-click the shape, choose Assign Macro, select an eligible macro, and confirm. Shapes offer more design flexibility; the macro still needs to exist and be available when the workbook is used.
Why is my macro not listed under Assign Macro?
It may be in a closed workbook, in the wrong module type, declared Private, or written with required arguments. A standard-module Public Sub with no required arguments is the usual choice for direct assignment.
Can a button run a macro in Excel for the web?
No. Excel for the web does not run VBA macros. Open the macro-enabled workbook in the Excel desktop application to use its VBA button.
Can I undo a macro after clicking the button?
Usually not with Excel’s Undo command. VBA operations generally clear or bypass the normal undo history, so save a separate copy before running code that might change important data.