When you assign different macros to drawing objects, group two or more of the drawing objects on a worksheet, attempt to create a group macro, and then click an object in the group, Microsoft Excel runs the macro that is assigned to the object you clicked, as in the following example:
- Start Excel.
- Press ALT+F11 to start the Visual Basic editor.
- On the Insert menu, click Module.
- In the module sheet, type the following code:
Sub Oval_Code()
MsgBox "This is an oval."
End Sub
Sub Rectangle_Code()
MsgBox "This is a rectangle."
End Sub
Sub Group_Code()
MsgBox "This is a group."
End Sub
- Press ALT+F11 to return to Excel.
- On the Drawing toolbar, click Oval, and then draw an oval on the worksheet.
NOTE: If the Drawing toolbar is not visible, point to Toolbars on the View menu, and then click Drawing.
- On the Drawing toolbar, click Rectangle, and then draw a rectangle on the worksheet.
- Right-click the oval, and then click Assign Macro on the shortcut menu.
- In the Assign Macro dialog box, click Oval_Code, and then click OK.
- Right-click the rectangle and then click Assign Macro on the shortcut menu.
- In the Assign Macro dialog box, click Rectangle_Code, and then click OK.
- Press and hold CTRL+SHIFT, click the rectangle, and then click the circle.
Both objects are selected. - On the Draw menu, click Group.
- Right-click either object to select the group, and then click Assign Macro on the shortcut menu.
- In the Assign Macro dialog box, click Group_Code, and then click OK.
- Click outside the group to unselect the group.
- Click the rectangle.
Note that the macro assigned to the rectangle appears, not the macro assigned to the group.