Code Modification Help Needed

lcodd6

New Member
Joined
Aug 31, 2016
Messages
6
I need help modifying this code. When I use this code I need it to resize the picture to fit into the selected cell but keep the same aspect ratio. Also, is it possible to have the code compress the picture to reduce the file size?

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 = True
.Top = r.Top
.Left = r.Left
.Height = r.RowHeight
End With
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
When pasting code, please do so between code tags. Click the # icon on the toolbar to insert them.

You can not "fit" in a cell AND keep the ratios the same as a general rule. It can be done for one cell or a few if not in same row or column. Doing so for many in a column is likely not possible since the pics may have different ratios.

Since you used Insert, picsize should not be an issue as it links the file rather than embed. I have seen compress done using Application.SendKeys() but using it is seldom a good idea. That method also depends on your Excel version. You would be better off making copies of the files and using a 3rd party program to reduce file size.
 
Upvote 0
Code:
[CODE][CODE][CODE]
[/CODE][/CODE][/CODE]
When pasting code, please do so between code tags. Click the # icon on the toolbar to insert them.

You can not "fit" in a cell AND keep the ratios the same as a general rule. It can be done for one cell or a few if not in same row or column. Doing so for many in a column is likely not possible since the pics may have different ratios.

Since you used Insert, picsize should not be an issue as it links the file rather than embed. I have seen compress done using Application.SendKeys() but using it is seldom a good idea. That method also depends on your Excel version. You would be better off making copies of the files and using a 3rd party program to reduce file size.

Can you help me modify this code to "fit into the cell?

Code:
[COLOR=#333333][COLOR=#333333][COLOR=#333333]Sub piccy()[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]Dim sFile As Variant, r As Range[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]sFile = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.bmp), *.jpg;*.bmp", Title:="Browse to select a picture")[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]If sFile = False Then Exit Sub[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]On Error Resume Next[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]Set r = Application.InputBox("Click in the cell to hold the picture", Type:=8)[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]On Error GoTo 0[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]If r Is Nothing Then Exit Sub[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]If r.Count > 1 Then Exit Sub[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]ActiveSheet.Pictures.Insert (sFile)[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]With ActiveSheet.Shapes(ActiveSheet.Shapes.Count)[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333].LockAspectRatio = True[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333].Top = r.Top[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333].Left = r.Left[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333].Height = r.RowHeight[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]End With[/COLOR][/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333][COLOR=#333333]End Sub[/COLOR][/COLOR][/COLOR]
 
Upvote 0
Sure, just remove your LockAspectRatio and set the width as I show below. You can link or not using AddPicture(). e.g.
Code:
Sub Main()
  Dim pic As Shape, r As Range, fPath As String

  fPath = "x:\pics\"
  
  On Error Resume Next
  For Each r In Range("A2", Range("A" & Rows.Count).End(xlUp))
    Set pic = ActiveSheet.Shapes.AddPicture(Filename:=fPath & r.Value2 & ".jpg", _
      LinkToFile:=msoFalse, SaveWithDocument:=msoCTrue, _
      ******=r.Offset(, 1).Left, *****=r.Offset(, 1).Top, _
      *******=r.Width, ********=r.RowHeight)
    'Debug.Print pic.Name
  Next r
End Sub
Since the forum is obfuscating parts, see Main() in: https://www.dropbox.com/s/f6atwnu81spirsy/InsertAndResize2.xlsm?dl=0
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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