inserting pictures

gabethegrape

New Member
Joined
Mar 3, 2009
Messages
38
I've got the following macro to insert pictures into a worksheet. I want it to do the following:

1. Keep the picture within a maximum size parameter (206 x 206)
2. If picture height is greater than picture width, minimize picture height to 206 and keep width ratio.
3. If picture width is greater than picture height, minimize picture width to 206 and keep width ratio.

My current macro only preserves the height. It works for pictures that are taller than they are wider. If I add width parameter to the code then it compresses the picture (which I don't want).

Can anyone help me out??

Cheers,
Gabe



Sub InsertPicture2()
Dim myPicture As String
Dim pic As Picture

myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = "False" Then Exit Sub

If myPicture <> "" Then
Set r = Range("c21")
Set pic = ActiveSheet.Pictures.Insert(myPicture)

With pic
.Top = r.Top
.Left = r.Left
.ShapeRange.LockAspectRatio = msoFalse
.Height = 206
.Placement = xlMoveAndSize
End With
End If
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Gabe,

This might not be the exact result you want but I tweak the code posted a little. I made the assumption that if the image is smaller than 206px x 206px that you wanted to enlarge the image to the max size without exceeding 206px. If you don't want images inlarged to your maximum allowable size, you may need to nest a few more if statements to get the desired results

Code:
Sub InsertPicture2()
Dim myPicture As String
Dim pic As Picture

myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = "False" Then Exit Sub

If myPicture <> "" Then
Set r = Selection    'Range("c21")
Set pic = ActiveSheet.Pictures.Insert(myPicture)


    If pic.Height > pic.Width Then
        With pic
            .Top = r.Top
            .Left = r.Left
            .ShapeRange.LockAspectRatio = msoCTrue
            .Height = 206
            .Placement = xlMoveAndSize
        End With
    ElseIf pic.Width > pic.Height Then
        With pic
            .Top = r.Top
            .Left = r.Left
            .ShapeRange.LockAspectRatio = msoCTrue
            .Width = 206
            .Placement = xlMoveAndSize
        End With
    End If

End If
End Sub
HTH,
J Ericson
 
Upvote 0
Thanks, J! That worked really well. I just added one more condition for pictures of equal size:

Sub InsertPicture2()
Dim myPicture As String
Dim pic As Picture

myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = "False" Then Exit Sub

If myPicture <> "" Then
Set r = Selection 'Range("c21")
Set pic = ActiveSheet.Pictures.Insert(myPicture)


If pic.Height > pic.Width Then
With pic
.Top = r.Top
.Left = r.Left
.ShapeRange.LockAspectRatio = msoCTrue
.Height = 206
.Placement = xlMoveAndSize
End With
ElseIf pic.Width > pic.Height Then
With pic
.Top = r.Top
.Left = r.Left
.ShapeRange.LockAspectRatio = msoCTrue
.Width = 206
.Placement = xlMoveAndSize
End With
ElseIf pic.Height = pic.Width Then
With pic
.Top = r.Top
.Left = r.Left
.ShapeRange.LockAspectRatio = msoCTrue
.Height = 206
.Placement = xlMoveAndSize
End With
End If

End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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