How does Excel name the images that are copied from a specific range?

warviksam

New Member
Joined
May 25, 2020
Messages
4
Office Version
  1. 365
  2. 2019
  3. 2010
Platform
  1. Windows
Hi,

I am trying to copy a data from a range and paste it as "linked image" in another range. Excel seems to define a new name for the image as it gets pasted each time. How do I define a name for this image?

What I want to do is to resize the pasted image to fit in a defined range. Please help me with the code to resize the image also. I have pasted what I have written so far.

VBA Code:
Sub copy_image()

Set wsb = Worksheets("x")
Set wso = Worksheets("y")

wsb.Activate
Range("C1:H18").Select
Selection.Copy
wso.Select
Range("AG64:AY81").Select
wso.Pictures.Paste(Link:=True).Select
ActiveSheet.Shapes.Range(Array("Picture 104")).Select '--> Picture 104 is what was defined by excel for the last image it pasted. This is hard coded and cannot be used for anything else.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi and welcome to MrExcel

Try this:

VBA Code:
Sub copy_image()
  Dim img As Picture, sh As Worksheet
  Set sh = Sheets("y")
  
  'Delete previous image to add the new one and not have many images on it
  On Error Resume Next
  sh.Shapes("Picture y").Delete
  On Error GoTo 0
  
  'Copy and paste image
  Sheets("x").Range("C1:H18").Copy
  Set img = sh.Pictures.Paste(Link:=True)
  img.Name = "Picture y"
  
  'resize the image
  With sh.Range("AG64:AY81")
    img.Top = .Top
    img.Left = .Left
    img.Height = .Height
    img.Width = .Width
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,560
Messages
6,120,222
Members
448,951
Latest member
jennlynn

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