Excel VBA Macro Button: Add, Assign, and Troubleshoot
In this article
An Excel VBA macro button is a clickable control that runs a macro when you select it. In Excel for Microsoft 365 on Windows, the simplest method is to add the Developer tab, choose Developer > Insert > Button (Form Control), draw the button on a worksheet, and assign an existing macro or create one.
What a macro button does
A button provides a visible way to run VBA code without opening the Visual Basic Editor or using the Macros dialog. It is useful for repeatable actions such as formatting a report, clearing an entry form, refreshing a workbook, or copying completed data to an archive. The button itself does not contain the macro; it is linked to a procedure stored in the workbook or another available VBA project.
For example, a worksheet could have a button labeled “Format Report” assigned to a macro that applies a heading style and adjusts column widths. Selecting the button runs the linked procedure. A button does not make a macro safer or more restricted: the assigned code runs with the permissions available to Excel, so only use macros you trust.
Show the Developer tab
The Developer tab is often hidden by default. To display it in Excel for Microsoft 365 on Windows, open File > Options > Customize Ribbon. In the list of Main Tabs, select Developer, then choose OK. The tab provides access to controls, macro tools, and the Visual Basic Editor.
On Excel for Mac, enable the tab through Excel > Settings (or Preferences, depending on the release) > Ribbon & Toolbar, then turn on Developer. Excel for the web can open and edit many workbook contents, but it cannot create, run, or edit VBA macros. Use the desktop app for those tasks.
Add and assign a Form Control button
- Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm) using File > Save As. A standard .xlsx file does not retain VBA code when saved.
- Choose Developer > Insert. Under Form Controls, select Button (Form Control). Do not confuse it with ActiveX controls, which are a separate option and are not supported in Excel for Mac.
- Drag on the worksheet to draw the button. When you release the mouse, the Assign Macro dialog appears.
- Select the macro to run and choose OK. If the macro has not been written yet, choose New to open the Visual Basic Editor and create the procedure.
- Right-click the button to edit its text, assign a different macro, or adjust its size and placement. Choose Edit Text to change its displayed label.
For instance, if a public, parameter-free procedure named FormatReport exists in a standard module, select FormatReport in the Assign Macro dialog. The button can then run that procedure when clicked. Form Control buttons are generally the straightforward choice for a simple worksheet action and work across Windows and Mac desktop versions that support VBA.
Write a simple macro for the button
A macro assigned to a worksheet button is usually a Public Sub procedure with no required arguments, stored in a standard module. Open the Visual Basic Editor with Developer > Visual Basic, choose Insert > Module, and add code such as this example. It formats the active sheet’s first row; it does not change cell values.
Public Sub FormatReport()
' Format the first row of the active worksheet.
With ActiveSheet.Rows(1)
.Font.Bold = True
.Interior.Color = RGB(217, 225, 242)
End With
ActiveSheet.Columns.AutoFit
End Sub
Before running a macro, save a copy of the workbook. VBA actions cannot be reversed with Ctrl+Z, so a mistake may require closing without saving or restoring a backup. Also consider making the code more specific: using ActiveSheet means the macro affects whichever worksheet is active when the button is clicked. To target a known sheet, refer to it explicitly in the code and confirm that the sheet name is correct.
Form Control, shape, or ActiveX control?
For most simple tasks, a Form Control button is the least complicated option. A shape can also act as a button: insert a shape from Insert > Shapes, right-click it, choose Assign Macro, and select the procedure. Shapes offer more freedom over appearance, but the macro still needs to be assigned separately.
ActiveX buttons allow additional event-based customization on Windows, but they involve more setup and are not supported on Excel for Mac. They may also be restricted by security settings. Choose an ActiveX control only when its extra capabilities are genuinely needed and the workbook will be used in a compatible Windows environment. For a button that simply runs one macro, a Form Control or assigned shape is usually more portable.
Security, compatibility, and common problems
Excel may block macros from files downloaded from the internet or received from an untrusted location. Do not enable macros just because a workbook displays a warning or contains a button. Verify the source and understand what the VBA code does. Follow your organization’s security policies; avoid weakening macro security globally to make one workbook run.
- The button does nothing: Right-click it and choose Assign Macro to verify that the intended procedure is selected. Confirm that macros are enabled for this trusted file and that the procedure is available.
- The macro is missing: Check that the workbook was saved as .xlsm and that the procedure is in a standard module. A procedure marked Private or one requiring arguments may not appear as an assignable button macro.
- The button enters text-edit mode: Right-click the control first to open its context menu. A normal left-click runs the assigned macro when the sheet is not in design or editing mode.
- The code fails on another computer: Check for references to local file paths, missing add-ins, different worksheet names, or Windows-only features. VBA compatibility and security settings can differ between computers.
A macro-enabled workbook can be opened in Excel for the web, but its VBA macros cannot run there. A user may need to open the file in desktop Excel. VBA is also different from Office Scripts, which are a separate automation option available in supported Microsoft 365 environments.
Frequently Asked Questions
How do I make a button run a macro in Excel?
Show the Developer tab, choose Developer > Insert > Button (Form Control), draw the button, and select the macro in the Assign Macro dialog. Save the workbook as .xlsm to retain VBA.
Can I assign more than one macro to a button?
A Form Control button is assigned to one macro at a time. If you need several actions from one click, create a separate procedure that calls the other procedures in the desired order, then assign that procedure to the button.
Can I use a macro button in Excel for the web?
No. Excel for the web does not run VBA macros or provide the desktop VBA editing tools. Open the workbook in Excel for Windows or Mac to use VBA.
Why does Excel remove my macro?
The workbook may have been saved in a format that does not support VBA, such as .xlsx. Use Excel Macro-Enabled Workbook (*.xlsm) and confirm the saved file has the .xlsm extension.
Can a shape replace a macro button?
Yes. Insert a shape, right-click it, choose Assign Macro, and select the procedure. This is a useful choice when you want a more customized visual design.