Add a Button and Assign a Macro in Excel
In Excel, Buttons are used to call Macros. This tutorial will cover how to create Excel buttons, assign Macros to them, adjust their properties, and more.
By default, Excel macros are accessible in a list via the “Macros” button on the View ribbon.
Often though, you’ll want to provide easy access to a particular macro directly on your worksheet. This can be achieved using a Button control.
A Button control looks like a Microsoft Windows button, and runs a macro when clicked. It’s a much handier way to access your most commonly used macros, and is an easy way to expose custom functionality to other users of your workbook.
Run a Macro From a Button
To run a Macro from a button in Excel, simply click the button:
The Excel Developer Tab
Buttons are accessible via the Developer Tab.
Unfortunately, Excel hides the Developer tab by default. If you don’t see the Developer Ribbon, follow these steps:
- Click File > Options in the list on the left-hand border
- In the Options dialog select Customize Ribbon > Customize the Ribbon > Main Tabs and add a check-mark in the box for “Developer”, and click OK.
Add a Macro Button
In Excel, select the Developer tab, then click on the “Insert” dropdown in the Controls section. There are several types of controls divided into two sections, “Form Controls” and “ActiveX Controls”.
For now, just click on the Button control under “Form Controls”. Next, move the mouse anywhere over the worksheet surface, then hold left-click and drag the mouse to draw the outline of a rectangle. When you release left-click, a new dialog will appear titled “Assign Macro”.
Assigning a Macro to a Button
Here you can assign an existing Macro to the button, record a new macro, create a new macro from scratch using VBA, or click “Cancel” and return to your button later.
Assign Existing Macro to a Button
To assign an existing Macro, you simply select the macro’s name in the list, then click OK.
Edit an Existing Macro Before Assigning to a Button
To edit a macro before assigning it to the button, select the macro’s name in the list and click the “Edit” button (the “New” button text changes to “Edit”).
Record a Macro and Assign to Button
To record a new macro and assign it to the button, click “Record…”. This brings up the Record Macro dialog, where you specify a name and click “OK”. The button will be assigned that macro. Meanwhile, Excel will remain in a recording state until you click “Stop Recording” in the “Code” section of the Developer tab.
Write VBA Procedure and Assign to Button
To write a new macro for the button, type a new name for your macro in the textbox at the top of the dialog, then click “New”. Excel will bring up the VB Editor, in which you’ll see a new empty macro procedure with the name you entered. This procedure will be stored in a new module, visible in the Project window.
Change Macro Assigned to Button
To change the Macro that’s assigned to a button, simply right-click the button and select Assign Macro:
Here you can see the assigned Macro and make any desired changes.
How to Adjust Button Properties in Excel
Move or Resize Excel Button
After you’ve placed a button, you can easily move or resize it. To perform any of these actions, right-click on the button. Then you can left-click and drag the button to your desired location or resize it.
With the button selected, left-click on the button text to edit.
To add multiple lines, simple press the ENTER key.
To format other button properties, Right-Click > Format Control
Here you can adjust font sizes, and many other button properties:
Of particular note is the “Properties” tab, which changes how the button behaves as surrounding rows and columns are inserted, deleted, resized, or hidden/unhidden.
- Move and size with cells: The button will move and resize when rows and columns are changed.
- Move but don’t size with cells: The button will move, but not resize.
- Don’t move or size with cells: The button will not move or resize.
- Finally, Print Object can set the object to appear on printouts. This is unchecked by default, but can be toggled on if desired.
Assign a Macro to a Shape
Besides buttons, macros can assigned to other objects like Pictures, Textboxes, and Shapes. With a Picture or Shape, you can make a button that looks any way you like. Excel includes a wide variety of customizable Shapes including polygons, arrows, banners, and more that may be better suited to your worksheet than a regular button control.
Shapes are accessed from the Insert tab:
Select the shape you want from the Shape dropdown, draw it onto your worksheet as you would a button control, then right-click it and select “Assign Macro…” from the pop-up dialog. The options are the same as assigning a macro to a button.
Assign a Macro to a Hyperlink
Macros can also be assigned to hyperlinks by using VBA Events. Events are procedures that are triggered when certain actions are performed:
- Open/Close/Save Workbook
- Activate / Deactivate Worksheet
- Cell Values Change
- Click Hyperlink
- and more.
Events require knowledge of VBA. To learn more about events, visit our VBA Tutorial.