Inserted image to stretch to cell range

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
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
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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
 
Upvote 0
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.
 
Upvote 0
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: 11
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,814
Members
448,990
Latest member
rohitsomani

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