vba assign macro to shape in Excel

Q189. In Excel, how do I assign a macro to a shape using VBA?

For example, I have a macro "GenerateReport" in my WorkBook. I want to assign a macro to Shape "Rectangle 1" using VBA.

To do it in Excel, here is the answer:

  1. Option Explicit
  2. Sub AddMacrotoShape()
  3. ''
  4. ActiveSheet.Shapes("Rectangle 1").OnAction = "GenerateReport"
  5. ''
  6. End Sub


a) OnAction method is used to assign a macro to shape.


You can find similar Excel Questions and Answer hereunder

1) How do I update my DropDown list whenever the sheet is activated?

2) How can I dynamically add series to an existing chart using VBA?

3) How can I edit names of all the Shapes in the WorkSheet easily instead of navigating to each Shape and renaming them?

4) How can worksheet functions be accessed in VBA?

5) How can I get the count of number of series in a Chart using VBA?

6) How can I get users to select a file for processing using my macro?

7) How can I add a legend to a chart using VBA?

8) How to disable ability to insert Rows and Columns in Excel (using VBA)?

9) Determine if hyperlinks are valid in Excel

10) Calling a macro from another workbook in Excel

11) Line break in vba message box in Excel

12) How can I add a WorkSheet and name it as required using VBA?

13) Vba delete entire row if contains certain text in Excel

14) How can I extract file name from a full path including folder path and file name?

15) Vba to return week numbers in Excel