Rezise image to fit cell but keeping aspect ratio

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm looking at a way to resize an image placed into a cell so that it does not exceed the cell height or width whilst maintaining it's aspect ratio, in other words as soon as the height or width meets the cell width or height then it stops resizing.

I've found this in another post which goes some way, but this fills the whole cell and ignores the aspect ratio - does anyone know of how I could amend this to do what I need?

Code:
Sub piccy()
 Dim sFile As Variant, r As Range
 sFile = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture")
 If sFile = False Then Exit Sub
 On Error Resume Next
 Set r = Application.InputBox("Click in the cell to hold the picture", Type:=8)
 On Error GoTo 0
 If r Is Nothing Then Exit Sub
 If r.Count > 1 Then Exit Sub
 ActiveSheet.Pictures.Insert (sFile)
 With ActiveSheet.Shapes(ActiveSheet.Shapes.Count)
 .LockAspectRatio = False
 .Top = r.Top
 .Left = r.Left
 .Height = r.RowHeight * r.MergeArea.Rows.Count
 .Width = r.ColumnWidth * r.MergeArea.Rows.Count
 End With
 End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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