Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Click Event on ShapeRange

0

Hello,

Is there a way to click on the images ?

Like a click_event or something...

ActiveSheet.Pictures.Insert(PicPath).Select
        With Selection.ShapeRange
                .LockAspectRatio = msoFalse
                .Left = Sheet11.Range("L" & SelRow).Left
                .Top = Sheet11.Range("L" & SelRow).Top
                .Width = 50
                .Height = 50
                .Name = "EquipPic" & EquipRow 
        End With

To show the image on bigger scale

Answer
Discuss

Answers

0
Selected Answer

Generally, you can select the image (or any shape), right click and "Assign Macro...". Pick your (existing) macro name and next time you click on that object, the macro will run.

In your case, you want to act as if there was a Click event (but there isn't one for images and shapes). Instead you can use the .OnAction property of the image as follows: 1) create macro to be acted on image/shape click, 2) load the image (or create shape) 3) change its property called .OnAction = "<<macro name>>"

For you (with a loaded image 150 x150), add this sub to your module:

Sub ChangeImageSize()

    With ActiveSheet.Shapes(Application.Caller)

    If .Width <= 200 Then 'if it's the small, original sixe, make it 3 x bigger

        .Width = 450

        .Height = 450

      Else   'if it's the bigger size, shrink to original 150x150

        .Width = 150

        .Height = 150

    End If

    End With

End Sub

Then in your code above, change it to With Selection and add the line in bold (which assigns the macro above to the OnAction):

ActiveSheet.Pictures.Insert(PicPath).Select
        With Selection ' .ShapeRange
                .LockAspectRatio = msoFalse
                .Left = Sheet11.Range("L" & SelRow).Left
                .Top = Sheet11.Range("L" & SelRow).Top
                .Width = Sheet11.Range("L" & SelRow).Width
                '.Height = Sheet11.Range("L" & SelRow).Height
                .Name = "EquipPic" & SelRow 'don't call shapes the same thing!
                .OnAction = "ChangeImageSize"
        End With

Each image added by the macro will now expand or shrink in size on clicking (until deleted). Only problem is that theimage is selcted and you need to click off it first (so the sizing handles disappear) before your first click to expand (or shrink) the image. 

Attached is my file from yesterday but with those changes made. Again you'll need to change the images list in the CustomerItems sheet 

Discuss

Discussion

Hi John_Ru
Omg this is just amazing
Thanks.
GhostofWanted (rep: 46) Nov 30, '20 at 8:02 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login