Detect image selection and copy image in vba

JWDeiley

New Member
Joined
May 20, 2022
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Good morning.
I am stumped. I can't seem to figure out how to do this. On the image below you'll see that I have 6 pictures, none of which are selected. What I need to do is detect which image is selected by the user and then copy that image to another sheet. I can't seem to 'detect' the selection process. I would appreciate any help that you can give me.
Thank you,
JD
 

Attachments

  • Untitled.png
    Untitled.png
    143 KB · Views: 18

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Use the Selection property of the the Application object to refer to the object selected. Then use the TypeName function to determine the type of object select. Here's an example . . .

VBA Code:
    If TypeName(Application.Selection) <> "Picture" Then
        MsgBox "Please select a picture, and try again!", vbExclamation
        Exit Sub
    End If

Note, you can use the Selection property without a qualifier . . .

VBA Code:
    If TypeName(Selection) <> "Picture" Then

To copy the selected picture and paste it into another sheet, let's say cell B2 in "Sheet2" . . .

VBA Code:
    Dim pic As Picture
    Set pic = Selection
    
    pic.Copy
    
    Worksheets("Sheet2").Paste Worksheets("Sheet2").Range("B2")

Note, you can use the With/End With statement for your paste operation . . .

VBA Code:
    With Worksheets("Sheet2")
        .Paste .Range("B2")
    End With

By the way, if you have other pictures in addition to those 6 pictures, you'll want to first check that the selected picture is in fact one of those 6 pictures. In this case, you can use the TopLeftCell property of the Picture object. For example, to check whether the top left cell of the picture lies in Row 12 . . .

VBA Code:
    If pic.TopLeftCell.Row = 12 Then

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,268
Members
448,558
Latest member
aivin

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