Inserted image to stretch to cell range

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
780
Hello I insert the photo to appear in cell B8 at its original size.

How do I adapt the code below to make it perfectly fit over the range B8:F32?

Thanks.

VBA Code:
Sub Fit_to_Range()

Dim Pic_Location As String
Dim Pic_Name As String
'Set CellLoc as the cell that you want to fit the image into or any cell within the merged area you wish to place the image
Set CellLoc = Range("B8")
Pic_Name = "Front Page"
Pic_Location = "C:\Users\OneDrive - photos\22l\" & Pic_Name & ".jpg"
    Set Pic = CellLoc.Parent.Pictures.Insert(Pic_Location)
With Pic
.Top = Range("B8").Top
.Left = Range("B8").Left
.Height = Range("B32").Top - Range("B8").Top
.Width = Range("F8").Left - Range("F8").Left
End With

End Sub
 

Some videos you may like

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
413
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,

Mark LockAspectRatio to false.

VBA Code:
Dim Pic_Location As String
Dim Pic_Name As String
'Set CellLoc as the cell that you want to fit the image into or any cell within the merged area you wish to place the image
Set CellLoc = Range("B8")
Pic_Name = "Front Page"
Pic_Location = "C:\Users\OneDrive - photos\22l\" & Pic_Name & ".jpg"
    Set Pic = CellLoc.Parent.Pictures.Insert(Pic_Location)
With Pic
    .ShapeRange.LockAspectRatio = msoFalse
    .Top = Range("B8").Top
    .Left = Range("B8").Left
    .Width = Range("F8").Left - Range("B8").Left
    .Height = Range("B32").Top - Range("B8").Top
End With
 

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
780
Thanks, it moves the photo across and it isn't the right size or in the correct location.

It needs to be B8:F32.

Total row height is: 25*14.5
Total width is: 5*8.55

Any ideas?

Thanks.
 

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
413
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, Is the attached image not the required output ?

VBA Code:
Sub Fit_to_Range()

Dim Pic_Location As String
Dim Pic_Name As String
'Set CellLoc as the cell that you want to fit the image into or any cell within the merged area you wish to place the image
Set CellLoc = Range("B8")
Pic_Name = "Front Page"
Pic_Location = "C:\Users\OneDrive - photos\22l\" & Pic_Name & ".jpg"
    
Set Pic = CellLoc.Parent.Pictures.Insert(Pic_Location)
With Pic
    .ShapeRange.LockAspectRatio = msoFalse
    .Top = Range("B8").Top
    .Left = Range("B8").Left
    .Width = Range("G8").Left - Range("B8").Left
    .Height = Range("B33").Top - Range("B8").Top
End With

End Sub
 

Attachments

  • output.JPG
    output.JPG
    54.3 KB · Views: 4

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
780
That is the desired output, but it isn't working, it is ending up in column J?
 

Watch MrExcel Video

Forum statistics

Threads
1,128,020
Messages
5,628,181
Members
416,297
Latest member
Kara Payne

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
Top