Scale Inserted Image both Height and Width

meirbabboon

New Member
Joined
May 5, 2015
Messages
7
I have modified a macro that I found and have one issue with it. The macro will bring a picture into a selected area (merged cell) and scale to fit inside the boundaries of the merged cell, however if the width coming in matches the width of the range it does not scale the height. What I need it to do it scale both no matter what so the picture will fit correctly. See the attached macro. Any help would be appreciated. Thank you.
Code:
Sub AddPic()
Dim myPicture As String, pic As Object, MyRange As Range
If ActiveSheet.Pictures.Count > 0 Then
        rsp = MsgBox("There is an existing picture. " & vbCr _
            & "Do you wish to delete it ?", vbYesNoCancel)
        If rsp = vbCancel Then Exit Sub
        If rsp = vbYes Then
            ActiveSheet.Pictures(1).Delete
        End If
    End If
 myPicture = Application.GetOpenFilename _
 ("Pictures (*.gif; *.jpg; *.bmp; *.tif; *.bmp; *.png), *.gif; *.jpg; *.bmp; *.tif; *.bmp; *.png", _
 , "Select Picture to Import")
 If myPicture = "False" Then Exit Sub


 Set MyRange = Application.InputBox("Select your range...", Type:=8)
 InsertAndSizePic MyRange, myPicture
End Sub


Sub InsertAndSizePic(Target As Range, myPicture As String)
 Dim pic As Object
 Application.ScreenUpdating = False
Set pic = ActiveSheet.Shapes.AddPicture(myPicture, msoFalse, msoTrue, 0, 0, -1, -1)


    pic.ScaleHeight 1, msoTrue
    pic.ScaleWidth 1, msoTrue


 If Target.Cells.Count = 1 Then Set Target = Target.MergeArea
 With Target
     
    If pic.Width <= pic.Height Then
    pic.Height = .Height
    Else: pic.Width = .Width
    
    End If
  pic.Top = .Top - ((pic.Height - .Height) / 2)
  pic.Left = .Left - ((pic.Width - .Width) / 2)
  
 End With


Set pic = Nothing
    Dim DrObj
Dim Pict
Set DrObj = ActiveSheet.DrawingObjects
    For Each Pict In DrObj
      If Left(Pict.Name, 7) = "Picture" Then
        ActiveSheet.Shapes(Pict.Name).OLEFormat.Object.Border.ColorIndex = 1
        ActiveSheet.Shapes(Pict.Name).OLEFormat.Object.Border.Weight = 3
      End If
    Next


End Sub
 
Last edited by a moderator:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Change this line...
If pic.Width <= pic.Height Then

To this...
If pic.Width / pic.Height < Target.Width / Target.Height Then
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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