Using One Click Event for Multiple shapes (VBA)

RustamMika

New Member
Joined
Nov 21, 2023
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Could somebody show how to create class module for general event "Shape_Click"...
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Shapes don't have events, so you can't have a class which handles them.

Shapes have an OnAction property which is the name of a macro which is run when a shape is clicked. The first macro below assigns the Shape_Click macro to all the shapes on the active sheet.

VBA Code:
Public Sub Assign_Macro_For_All_Shapes()
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
        shp.OnAction = "Shape_Click"
    Next
End Sub

Public Sub Shape_Click()
    With ActiveSheet.Shapes(Application.Caller)
        MsgBox Application.Caller & " " & .TopLeftCell.Address & ":" & .BottomRightCell.Address
    End With
End Sub
 
Upvote 0
Solution
Shapes don't have events, so you can't have a class which handles them.

Shapes have an OnAction property which is the name of a macro which is run when a shape is clicked. The first macro below assigns the Shape_Click macro to all the shapes on the active sheet.

VBA Code:
Public Sub Assign_Macro_For_All_Shapes()
    Dim shp As Shape
    For Each shp In ActiveSheet.Shapes
        shp.OnAction = "Shape_Click"
    Next
End Sub

Public Sub Shape_Click()
    With ActiveSheet.Shapes(Application.Caller)
        MsgBox Application.Caller & " " & .TopLeftCell.Address & ":" & .BottomRightCell.Address
    End With
End Sub
Thank a lot John!
The problem solved...
 
Upvote 0

Forum statistics

Threads
1,224,891
Messages
6,181,614
Members
453,057
Latest member
LE102024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top