Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: How to Assign a Macro to a Button

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To assign a macro to a button in Excel, first make sure the macro exists in a standard module, then insert a Form Control button (Developer tab > Insert > Button), draw it on the sheet, and pick the macro from the Assign Macro dialog. If you prefer, you can right-click any existing shape or button and choose Assign Macro. This guide walks through the desktop version of Excel for Windows and Mac, where the Developer tab and VBA are available; the browser version cannot run macros.

What You'll Need

  • Excel desktop app (Microsoft 365, Excel 2019, 2021, or 2024) on Windows or Mac. Macros do not run in Excel for the web.
  • A workbook saved as .xlsm (Excel Macro-Enabled Workbook) if you want the macro to persist. Saving as .xlsx strips the VBA code.
  • The Developer tab enabled. If you do not see it, go to File > Options > Customize Ribbon (Windows) or Excel > Preferences > Ribbon & Toolbar (Mac) and tick Developer.
  • Macro security set to allow macros. In the Trust Center, choose Disable all macros with notification so you get a prompt, or enable macros for trusted locations only.
  • A macro already written in a standard module. If you have not written one yet, open the VBA editor with Alt+F11 (Windows) or Option+F11 (Mac), insert a module, and add a simple Sub.

Step-by-Step

  1. Open the workbook that contains the macro. The macro must live in the same workbook as the button, or in a referenced add-in. If the macro is in your Personal.xlsb, it will not appear in the Assign Macro list unless you choose All Open Workbooks.
  2. Save the file as .xlsm. Go to File > Save As and pick Excel Macro-Enabled Workbook (*.xlsm). Do this before adding buttons so you do not lose the macro when Excel warns about saving.
  3. Enable the Developer tab if it is hidden. Windows: File > Options > Customize Ribbon, tick Developer in the right-hand list, click OK. Mac: Excel > Preferences > Ribbon & Toolbar, tick Developer, close the window. Menu labels can differ slightly by Excel version and region.
  4. Insert a Form Control button. On the Developer tab, click Insert, then under Form Controls click the Button icon (the rectangle with a grey top). Your cursor becomes a crosshair. Drag on the sheet to draw the button. The Assign Macro dialog appears immediately.
  5. Select the macro. In the Assign Macro dialog, choose the macro name from the list. Macros appear as Module1.MyMacro or just MyMacro depending on how they were saved. Click OK. If the dialog does not appear, right-click the new button and choose Assign Macro.
  6. Rename the button. Right-click the button, choose Edit Text, and type a clear label such as Refresh Report or Send Email. Click outside the button to finish. The default label is Button 1, which is not helpful to anyone else using the sheet.
  7. Test the button. Click it once. If macro security is set to Disable all macros with notification, Excel shows a yellow security bar; click Enable Content first. The macro should run. If nothing happens, check that the macro is in a standard module, not a sheet module or ThisWorkbook.
  8. Position and format the button. Drag the button to move it, drag its handles to resize, and use the Format tab (which appears when the button is selected) to change fill, font, and alignment. To stop the button moving when cells are resized, right-click > Format Control > Properties and choose Move but don't size with cells.
  9. Assign a macro to an existing shape instead (optional). Insert any shape from Insert > Shapes, right-click it, choose Assign Macro, and pick the macro. This gives you more formatting freedom than a Form Control button. The same right-click method works for pictures and icons.
  10. Save and close. Save the .xlsm file. When you reopen it, click Enable Content on the security bar if prompted, then test the button again.

Common Mistakes

  • Saving as .xlsx. Excel warns that macro-free workbooks cannot store VBA. If you ignore the warning and save, the macro is deleted and the button will show a Cannot run the macro error.
  • Putting the macro in the wrong place. Code in a worksheet module or ThisWorkbook does not appear in the Assign Macro list. Move it to a standard module (Insert > Module in the VBA editor).
  • Forgetting to enable macros. A button that does nothing is often just blocked by macro security. Look for the yellow bar and click Enable Content.
  • Naming the macro badly. Macro names cannot contain spaces or start with a number. Use MyMacro or Refresh_Report, not My Macro or 1stMacro.
  • Deleting the macro but keeping the button. The button remains and throws an error when clicked. Right-click the button and reassign or delete it.
  • Using ActiveX buttons without need. ActiveX controls require the workbook to be trusted and can break on other machines. Form Control buttons are simpler and more portable.

Tips

  • Give every macro a descriptive name, and add a short comment at the top of the Sub so you remember what it does.
  • Use Alt+F8 (Windows) or Fn+Option+F8 (Mac) to open the Macro dialog and confirm the macro is visible before you assign it.
  • Hold Alt while dragging a button to align it precisely with cell edges.
  • If you need the same button on many sheets, put the macro in Personal.xlsb and assign it from All Open Workbooks.
  • Test the button after any change to macro security settings, because a previously working button can stop running.

Troubleshooting

If the button does nothing, first check the security bar and click Enable Content. Then press Alt+F8 and confirm the macro name appears; if it does not, the code is in the wrong module or the file was saved as .xlsx. If you see the error Cannot run the macro, the assigned name no longer exists; right-click the button and reassign it. If the Assign Macro dialog is empty, open the VBA editor and check that the Sub is Public (no Private keyword) and sits in a standard module. If the button moves or resizes with cells, change its Format Control properties. If the workbook opens with macros disabled and no prompt, review Trust Center settings; do not lower security globally, use a trusted location instead. On Mac, some older ActiveX controls do not work at all; switch to a Form Control button or a shape.

Frequently Asked Questions

Can I assign a macro to a button in Excel for the web?

No. Excel for the web cannot run VBA macros. Open the workbook in the desktop app to use buttons and macros.

Why does my button show the wrong macro name?

The button is still linked to the old macro. Right-click it, choose Assign Macro, and select the correct one. Renaming a macro in the VBA editor breaks the link.

Can one button run several macros?

Not directly. Create a new macro that calls the others in order, then assign that wrapper macro to the button.

How do I remove a button?

Right-click the button and choose Cut, or click it and press Delete. This removes the button only, not the macro code.

Do buttons work on Mac?

Yes, Form Control buttons and shapes work in Excel for Mac. ActiveX controls are not supported on Mac.

Why does the button work for me but not my colleague?

Their Excel likely has macros disabled or the file is blocked. Ask them to click Enable Content, or place the file in a trusted location.