Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy embedded image without using Shapes

Tags:

excel

vba

I have a workbook with many sheets and images that have random names and arbitrary order, the only image that I need is always in the range A2:C14, I'm using the following code to copy the image:

firstSheet.Range("A2:C14").Copy
secondSheet.Range("I6").PasteSpecial

But it only copies the cell text, not the image.
Is there a way to copy an image using Range("A2:C14)" or another way to select the cell to copy the image?

like image 705
BernardoLima Avatar asked Mar 08 '26 00:03

BernardoLima


1 Answers

Since CopyPicture is a method of a Shape object, unless you already have a pointer to it, using the Shapes collection is unavoidable

Something like this

Sub Demo()
    Dim shp As Shape
    Dim rng As Range

    Set rng = firstSheet.Range("A2:C14")
    For Each shp In firstSheet.Shapes
        If Not Intersect(rng, shp.TopLeftCell) Is Nothing Then
            ' Found it
            shp.CopyPicture
            secondSheet.Range("I6").PasteSpecial

            Exit Sub
        End If
    Next
End Sub
like image 94
chris neilsen Avatar answered Mar 10 '26 15:03

chris neilsen