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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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