VBA Excel 2010 - Embedding Pictures and Resizing

前端 未结 1 1614
走了就别回头了
走了就别回头了 2020-12-11 04:39

I\'ve been lurking for a while and found it very helpful, so thanks for the help already!

I\'m trying to write a macro to embed images into a worksheet from individu

相关标签:
1条回答
  • 2020-12-11 05:04

    You first load and position the picture in its original size, and in a second step resize it as desired. You only specify EITHER width or heigth to retain the aspect ratio.

    Sub Test()
    Dim MySht As Worksheet
    Dim MyPic As Shape
    Dim MyLeft As Single, MyTop As Single
    
        ' position in Pixel relative to top/left of sheet
        MyTop = 50
        MyLeft = 50
    
        ' alternatively position to the top/left of [range] C3
        MyTop = [C3].Top
        MyLeft = [C3].Left
    
        ' alternatively position to top/left of actual scrolled position
        MyTop = Cells(Windows(1).ScrollRow, Windows(1).ScrollColumn).Top
        MyLeft = Cells(Windows(1).ScrollRow, Windows(1).ScrollColumn).Left
    
    
        Set MySht = ActiveSheet
        Set MyPic = MySht.Shapes.AddPicture("C:\Users\MikeD\Desktop\Untitled.png", _
                    msoFalse, msoTrue, MyLeft, MyTop, -1, -1)
        '      ^^^  LinkTo    SaveWith                -1 = keep size
    
        ' now resize pic
        MyPic.Height = 100
    
    End Sub
    

    ... and try to avoid .Select ... Dim the objects you need and use them.

    0 讨论(0)
提交回复
热议问题