Lesson 18 of 60 – Assigning a Macro to a Shape
30%

Assigning a Macro to a Shape

Excel allows you to insert shapes such as rectangles, circles, arrows, and other graphical objects into a worksheet. You can assign a macro to a shape so that clicking the shape runs the macro.

Note: In this lesson, you will learn how to insert a shape, assign a macro to it, change its appearance, and use shapes as controls in practical Excel applications.

1. What is a Shape in Excel?

A shape is a graphical object that can be inserted into an Excel worksheet.

Examples include:

  • Rectangle
  • Circle
  • Arrow
  • Rounded Rectangle
  • Callout
  • Flowchart shapes

Shapes can be used for designing worksheets and creating user-friendly controls.

2. Why Use a Shape for a Macro?

A shape can work like a clickable button. When a macro is assigned to it, clicking the shape runs that macro.

Click Shape
     ↓
Run Macro
     ↓
Perform Excel Task

This can make an Excel workbook easier to use.

3. Shape vs Button

Excel provides Form Control buttons for running macros, but shapes can also be used as clickable macro controls.

Shapes provide more design options because you can change their size, fill, outline, text, and other appearance properties.

4. Example of a Macro Shape

Suppose you have a macro named:

GenerateReport

You can insert a rectangle shape and write:

Generate Report

After assigning the macro, clicking the shape will run the GenerateReport macro.

5. Prepare a Macro

Before assigning a macro to a shape, create a macro that performs a task.

For example:

Sub WelcomeMessage()

    MsgBox "Welcome to Excel Automation!"

End Sub

This macro displays a message when it runs.

6. Open the Insert Tab

To insert a shape, go to the Insert tab on the Excel Ribbon.

Look for the Shapes option in the Illustrations group.

Insert → Shapes

7. Select a Shape

Click Insert → Shapes and choose the shape you want to use.

For a simple macro control, a Rectangle or Rounded Rectangle is commonly suitable.

8. Draw the Shape

After selecting a shape, click and drag on the worksheet to draw it.

You can control the size of the shape by dragging its edges or corners.

Insert Shape
     ↓
Click and Drag
     ↓
Shape Appears on Worksheet

9. Add Text to the Shape

You can add text inside a shape to explain what it does.

For example:

Generate Report

The text makes the purpose of the shape clear to the user.

10. Rename the Shape Text

Right-click the shape and choose the option to edit its text.

Enter a meaningful name based on the task that the macro performs.

Examples:

  • Calculate Result
  • Generate Report
  • Clear Data
  • Search Student

11. Open Assign Macro

After creating the shape, right-click it.

Choose Assign Macro from the context menu.

Right-click Shape
       ↓
Assign Macro

12. Select the Macro

The Assign Macro dialog box displays available macros.

Select the macro that you want the shape to run.

For example:

GenerateReport

Then click OK.

13. Test the Shape

After assigning the macro, click the shape.

Excel should run the assigned macro.

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

14. Change the Shape Color

You can change the fill color of the shape to make it visually attractive.

Select the shape and use the Shape Format options to change its appearance.

Choose a color that makes the shape easy to identify.

15. Change the Shape Outline

You can also change the outline of a shape.

The outline can be modified using the shape formatting options.

A suitable outline can help separate the macro control from other worksheet content.

16. Change Shape Size

The size of the shape can be adjusted according to the worksheet layout.

Make the shape large enough so users can easily click it.

Avoid making the shape unnecessarily large.

17. Move the Shape

You can move the shape to any suitable location on the worksheet.

For example, macro controls can be placed near the top of a report or beside a data-entry area.

18. Multiple Macro Shapes

You can create multiple shapes and assign different macros to each shape.

[ Add Student ]

[ Calculate Result ]

[ Generate Report ]

[ Clear Form ]

Each shape can perform a different task.

19. Shape for Data Entry

A shape can be assigned to a macro that performs a data-entry operation.

For example, a shape labeled Add Student can run a macro that stores student information in a worksheet.

20. Shape for Calculations

A shape can also run a calculation macro.

For example:

Calculate Result
       ↓
Run Macro
       ↓
Calculate Total
       ↓
Calculate Percentage

21. Shape for Report Generation

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

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

This provides an easy way for users to start the report process.

22. Advantages of Using Shapes

Shapes provide several useful features:

  • Easy to click
  • Easy to format
  • Can contain descriptive text
  • Can be resized
  • Can be moved around the worksheet
  • Can be connected to macros

23. Shape and Macro Relationship

The shape acts as a visual control, while the assigned macro performs the actual task.

Shape
  ↓
Assigned Macro
  ↓
VBA Instructions
  ↓
Excel Task

The shape provides a simple interface for the user.

24. Changing the Assigned Macro

You can change which macro a shape runs.

Right-click the shape and select Assign Macro.

Choose another macro from the list and click OK.

The shape will then run the newly assigned macro.

25. Testing a Macro Shape

Always test a shape after assigning a macro.

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

26. Best Practices

Follow these practices when using macro shapes:

  • Use meaningful text inside the shape.
  • Assign the correct macro.
  • Keep related controls together.
  • Make shapes easy to click.
  • Test every macro shape.
  • Save the workbook as an .xlsm file.

27. Complete Shape Assignment Workflow

The complete process is:

Create Macro
     ↓
Insert Shape
     ↓
Add Shape Text
     ↓
Right-click Shape
     ↓
Assign Macro
     ↓
Select Macro
     ↓
Click OK
     ↓
Test Shape

28. Macro Shapes in Practical Projects

Shapes can be useful when creating practical Excel applications such as a student management workbook.

For example:

┌─────────────────────┐
│    Add Student      │
└─────────────────────┘

┌─────────────────────┐
│  Calculate Result   │
└─────────────────────┘

┌─────────────────────┐
│   Generate Report   │
└─────────────────────┘

Each shape can be connected to a different macro.

29. Practical Exercise

Try the following exercise:

  1. Create a simple macro that displays a message.
  2. Go to the Insert tab.
  3. Click Shapes.
  4. Select a Rectangle shape.
  5. Draw the shape on the worksheet.
  6. Add the text Click Me.
  7. Right-click the shape.
  8. Select Assign Macro.
  9. Select your macro.
  10. Click OK.
  11. Click the shape.
  12. Check whether the macro runs.

30. Complete Understanding of Macro Shapes

An Excel shape can be used as a visual control for running a macro. You can insert a shape, add descriptive text, format its appearance, and assign a macro to it.

When the user clicks the shape, Excel runs the assigned macro. This makes shapes useful for creating simple and user-friendly Excel applications.

Insert Shape
      ↓
Add Text
      ↓
Assign Macro
      ↓
Click Shape
      ↓
Run Macro

📌 Key Points

  • Shapes are graphical objects that can be inserted into Excel worksheets.
  • A shape can be used as a clickable macro control.
  • You can assign a macro to a shape using Assign Macro.
  • Shape text can describe the task performed by the macro.
  • Shapes can be resized, moved, and formatted.
  • Multiple shapes can be connected to different macros.
  • Macro shapes are useful for creating user-friendly Excel applications.

🧠 Quick Quiz

Question: How can you assign a macro to an Excel shape?