Lock Aspect Ratio - portrait, one line of code needed, quick fix

micksid

New Member
Joined
Jun 7, 2011
Messages
11
below is my coding, it inserts a picture into a cell and fits to what ever the cell size is.

my cells are more landscape (fixed) than portrait, how can i still make the images fit into the cell height wise but keep a aspect ratio of a portrait picture if you get me

Sub InsertPicture1()
Dim myPicture As Variant
myPicture = Application.GetOpenFilename _
("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
If myPicture = False Then Exit Sub
Application.ScreenUpdating = False
Dim iLeft#, iTop#, iWidth#, iHeight#
With Range("B4")
iLeft = .Left
iTop = .Top
.Select
End With
Set myPicture = ActiveSheet.Pictures.Insert(myPicture)
With Range("b4")
iWidth = .Width: iHeight = .Height
End With
With myPicture
.Width = iWidth: .Height = iHeight
End With
Application.ScreenUpdating = True
Dim octl As CommandBarControl
With Selection
Set octl = Application.CommandBars.FindControl(ID:=6382)
Application.SendKeys "%e~"
Application.SendKeys "%a~"
Application.SendKeys "%w~"
octl.Execute
End With
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Why are you setting the width? Try:

Code:
    With myPicture
        .ShapeRange.LockAspectRatio = msoTrue
        .Height = iHeight
    End With
 
Upvote 0
Why are you setting the width? Try:

Code:
    With myPicture
        .ShapeRange.LockAspectRatio = msoTrue
        .Height = iHeight
    End With


thanks for that, it was some coding i found on the net and it worked ok, apart from the aspect ratio
can you modify my above code and put your bit into mine, ive tried putting it in, but it stretches the pic

so ive prob put it in the worng place

thanks
 
Upvote 0
Try this:

Code:
Sub InsertPicture1()
    Dim myPicture As Variant
    myPicture = Application.GetOpenFilename _
        ("Pictures (*.gif; *.jpg; *.bmp; *.tif),*.gif; *.jpg; *.bmp; *.tif", , "Select Picture to Import")
    If myPicture = False Then Exit Sub
    Application.ScreenUpdating = False
    Dim iLeft#, iTop#, iWidth#, iHeight#
    With Range("B4")
        iLeft = .Left
        iTop = .Top
        .Select
    End With
    Set myPicture = ActiveSheet.Pictures.Insert(myPicture)
    With Range("b4")
        iWidth = .Width: iHeight = .Height
    End With
    With myPicture
        .ShapeRange.LockAspectRatio = msoFalse
        .Width = .Width * iHeight / .Height
        .Height = iHeight
    End With
    Application.ScreenUpdating = True
    Dim octl As CommandBarControl
    With Selection
        Set octl = Application.CommandBars.FindControl(ID:=6382)
        Application.SendKeys "%e~"
        Application.SendKeys "%a~"
        Application.SendKeys "%w~"
        octl.Execute
    End With
End Sub
 
Upvote 0
thats works a treat thank you very much

dont suppose you know if you can centre the picture in the cells. at the moment it goes (top left) and you make it (Top Centre)

cheers

oh and this may be a stupid question, and i have tried it but it doesnt change automatically. i want to copy what ever is in cell a1 on sheet 1 to cell a1 on sheet 2.

i have done =sheet!A1 but it doesnt update automatically.. the cells will contain text
 
Upvote 0
To centre the picture:

Rich (BB code):
    With myPicture
        .ShapeRange.LockAspectRatio = msoFalse
        .Width = .Width * iHeight / .Height
        .Height = iHeight
        .Left = .Left + (iWidth - .Width) / 2
    End With

Your formula should update automatically if calculation is set to automatic. Otherwise you will need to press F9 when Calculate appears in the Status Bar.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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