调整VBA中的图像大小保持纵横比

时间:2017-11-08 18:31:17

标签: excel vba image excel-vba aspect-ratio

我有一个VBA宏,用于存储文件中的图像,并将其粘贴到Excel电子表格中,位于名为“Country Profile”的工作表中。 我想调整图像的大小,使其宽度为350像素,同时保持其宽高比。

这是我写的代码:

Public Sub Macro15()

Dim picname As String
Dim shp As Shape
Dim present As String

Sheets("Country Profile").Activate
Sheets("Country Profile").Select
ActiveSheet.Pictures.Delete

Cells(19, 40).Select 'This is where picture will be inserted (column)

picname = Sheets("REPORT ON").Range("A2").Value 'This is the picture name

Cells(19, 46).Select

            Call ActiveSheet.Shapes.AddPicture("C:\Users\" & Environ("UserName") & "\Maps with Cities\" & picname & ".png", _
            LockAspectRatio = msoTrue, msoCTrue, Left:=Cells(19, 40).Left, Top:=Cells(19, 46).Top, Width:=350, Height:=-1).Select

End Sub

代码有效,图像插入到所需的文件中。但是,不保持纵横比。我该怎么做才能纠正这个问题?

1 个答案:

答案 0 :(得分:2)

试试这样:

With ActiveSheet.Pictures.Insert(PicPath)
    .ShapeRange.LockAspectRatio = msoTrue
    .Width = 350
End With