简体   繁体   中英

Macro in Excel to Fit Image in Cell through Browsing

A macro us needed that will be assigned to a button. Upon clicking, browser should open to lead to the image file.

Once located, the image need be fitted inside Excel Cell, with maintaining its aspect ratio but not exceeding cell size.

For the said need, i have the following two codes that are mutually exclusive. That is, I cannot run them together to fulfill the above need.

First part

ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 191.25, 74.25, 49.5 _
    , 16.5).Select
With Selection.ShapeRange.Fill
    .Visible = msoTrue
    .PresetTextured msoTexturePapyrus
    .TextureTile = msoTrue
    .TextureOffsetX = 0
    .TextureOffsetY = 0
    .TextureHorizontalScale = 1
    .TextureVerticalScale = 1
    .TextureAlignment = msoTextureTopLeft
End With
With Selection.ShapeRange.Fill
    .Visible = msoTrue
    .UserPicture "C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
    .TextureTile = msoFalse
End With

Second part

ActiveSheet.Hyperlinks.Add Anchor:=Selection.ShapeRange.Item(1), Address:= _
    "C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
Range("C14").Select

Best regards Rehan

Here's the answer that works...

Sub Oval1_Click()
Dim my_file As String
my_file = Application.GetOpenFilename()
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 96, 44.25, 279.75, _
        16.5).Select
    Selection.Placement = xlMoveAndSize
    Application.CommandBars("Format Object").Visible = False
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .UserPicture _
        my_file
        .TextureTile = msoFalse
        .RotateWithObject = msoTrue
    End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM