Lesson 17 of 60 – Assigning a Macro to a Button
28%

Assigning a Macro to a Button

In Excel, you can create a button and assign a macro to it. When the user clicks the button, Excel runs the assigned macro automatically.

Note: In this lesson, you will learn how to create a button, assign a macro to it, run the macro using the button, and use buttons in practical Excel projects.

1. What is a Macro Button?

A macro button is a button in an Excel worksheet that is connected to a macro.

When the button is clicked, Excel executes the macro assigned to that button.

Click Button
     ↓
Run Macro
     ↓
Perform Task

2. Why Use a Macro Button?

A macro button provides an easy way to run a macro without opening the Macros dialog box every time.

Users can simply click a button to perform a repeated task.

For example:

  • Generate a report
  • Format a table
  • Clear data
  • Calculate results
  • Copy information

3. Example of a Macro Button

Suppose you have a macro named:

FormatReport

You can create a button named Format Report. When the user clicks the button, the FormatReport macro runs.

4. Prepare a Macro

Before assigning a macro to a button, you need to have a macro available.

For example, you can create a simple macro using the Macro Recorder.

Sub ShowMessage()

    MsgBox "Welcome to Excel Automation!"

End Sub

This macro displays a message when it runs.

5. Open the Developer Tab

The Developer tab contains the controls needed to create a macro button.

Click the Developer tab on the Excel Ribbon.

If the Developer tab is not visible, enable it from Excel Options.

6. Open the Insert Menu

In the Developer tab, find the Controls group.

Click Insert to see the available controls.

Excel provides different controls that can be placed on a worksheet.

7. Select the Button Control

Under Form Controls, select the Button control.

The button control allows you to create a clickable button on the worksheet.

Developer
   ↓
Insert
   ↓
Button (Form Control)

8. Draw the Button

After selecting the Button control, click and drag on the worksheet to create the button.

You can choose the location and size of the button according to your worksheet design.

9. Assign Macro Dialog Box

After creating the button, Excel opens the Assign Macro dialog box.

This dialog box displays the macros available in the workbook.

You can select the macro that you want to connect to the button.

10. Select a Macro

Select the macro that you want the button to run.

For example:

FormatReport

After selecting the macro, click OK.

11. Test the Button

After assigning the macro, click the button on the worksheet.

Excel should execute the assigned macro.

If the macro displays a message, the message box should appear when the button is clicked.

12. Rename the Button

The button text can be changed to make its purpose clear.

For example, change the default text to:

Generate Report

A clear button name helps users understand what the button does.

13. Edit Button Text

To edit the text displayed on the button, right-click the button and choose the option for editing its text.

You can then enter a meaningful label.

Examples:

  • Calculate Total
  • Generate Report
  • Clear Data
  • Print Report
  • Update Records

14. Assigning a Different Macro

You can change the macro assigned to an existing button.

Right-click the button and choose Assign Macro.

Select another macro and click OK.

15. One Button for One Task

A simple design approach is to use one button for one main task.

For example:

Calculate Total
Generate Report
Clear Form
Print Report

Each button can have a different macro assigned to it.

16. Multiple Buttons in One Worksheet

You can place multiple macro buttons on the same worksheet.

For example:

[ Add Student ]

[ Calculate Result ]

[ Generate Report ]

[ Clear Form ]

Each button can perform a different operation.

17. Button for Data Entry

A button can be used to run a macro that performs a data-entry task.

For example, a button named Add Student can run a macro that places student information into a worksheet.

18. Button for Calculations

A macro button can also run calculation-related macros.

For example, a button named Calculate Result can run a macro that calculates total marks and percentage.

Click Calculate Result
          ↓
Run Macro
          ↓
Calculate Total
          ↓
Calculate Percentage

19. Button for Report Generation

A report-generation macro can be assigned to a button.

For example, clicking Generate Report can run a macro that prepares a formatted report.

20. Button for Clearing Data

A button can also be connected to a macro that clears selected input cells.

For example:

Clear Form
     ↓
Run Macro
     ↓
Remove Previous Input

This can be useful in data-entry worksheets.

21. Button for Printing

A button can be assigned to a macro that prepares or prints a report.

For example, a button labeled Print Report can run a printing macro.

This can make frequently used reports easier to operate.

22. Advantages of Macro Buttons

Macro buttons provide several practical benefits:

  • Easy to use
  • Quick access to macros
  • Useful for beginners
  • Reduces repeated menu navigation
  • Can make worksheets easier to operate
  • Useful for Excel applications

23. Button and Macro Relationship

The button itself does not contain the complete macro logic. It is connected to a macro and starts that macro when clicked.

Button
   ↓
Assigned Macro
   ↓
VBA Instructions
   ↓
Excel Task

24. Common Mistake

One common mistake is creating a button without assigning the correct macro.

If the wrong macro is assigned, clicking the button will perform a different task than expected.

Always check the assigned macro before using the button in a project.

25. Testing a Macro Button

After creating a macro button, test it carefully.

  1. Click the button.
  2. Check whether the correct macro runs.
  3. Check the output.
  4. Test the button again if required.
  5. Confirm that it performs the intended task.

26. Best Practices

Follow these practices when using macro buttons:

  • Use meaningful button names.
  • Assign the correct macro.
  • Keep buttons organized.
  • Test every button.
  • Keep related buttons together.
  • Save the workbook as an .xlsm file.

27. Complete Button Creation Workflow

The complete process is:

Create Macro
     ↓
Developer Tab
     ↓
Insert Button
     ↓
Draw Button
     ↓
Assign Macro
     ↓
Click OK
     ↓
Rename Button
     ↓
Test Button

28. Macro Buttons in Practical Projects

Macro buttons are useful when creating practical Excel applications.

For example, a student management workbook may contain:

[ Add Student ]

[ Calculate Result ]

[ Search Student ]

[ Generate Report ]

[ Clear Form ]

Each button can be connected to a suitable macro.

29. Practical Exercise

Try the following exercise:

  1. Create a simple macro that formats a worksheet heading.
  2. Open the Developer tab.
  3. Select Insert → Button from Form Controls.
  4. Draw a button on the worksheet.
  5. Assign your formatting macro.
  6. Change the button text to Format Report.
  7. Click the button.
  8. Check whether the macro runs correctly.

This exercise will help you understand how a button can be connected to a macro.

30. Complete Understanding of Macro Buttons

A macro button provides a simple interface for running an Excel macro. Instead of opening the Macro dialog box, the user can click a button to execute the assigned macro.

Buttons are especially useful when building practical Excel applications where users need simple controls for repeated tasks.

Button
  ↓
Macro
  ↓
VBA Code
  ↓
Excel Task

📌 Key Points

  • A macro button is a clickable control connected to a macro.
  • Buttons can make macros easier to run.
  • The Button control is available under Developer → Insert → Form Controls.
  • After creating a button, you can assign a macro to it.
  • You can rename buttons to clearly describe their purpose.
  • Multiple buttons can be placed on one worksheet.
  • Macro buttons are useful in practical Excel applications.

🧠 Quick Quiz

Question: What happens when you click a button that has a macro assigned to it?