代码之家  ›  专栏  ›  技术社区  ›  franciscofcosta

在Excel VBA中调整图片大小

  •  1
  • franciscofcosta  · 技术社区  · 6 年前

    我有一个VBA脚本,它将获取图片并将其粘贴到Excel中的特定单元格中,具体取决于另一个单元格的值。

    Public Function PictureLookup(Value As String, Location As Range, Index As Integer)
    
    Application.Volatile
    
    Dim lookupPicture As Shape
    Dim sheetName As String
    Dim picTop As Double
    Dim picLeft As Double
    
    sheetName = Location.Parent.Name
    
    'Delete current picture with the same Index if exists
    For Each lookupPicture In Sheets(sheetName).Shapes
        If lookupPicture.Name = "PictureLookup" & Index Then
            lookupPicture.Delete
        End If
    Next lookupPicture
    
    'Get position of cell calling the UDF
    picTop = Location.Top
    picLeft = Location.Left
    
    'Add the picture in the right location
    Set lookupPicture = Sheets(sheetName).Shapes.AddPicture _
    ("G:\My Drive\MY FOLDER\LOGOS\" & Value & ".jpg", msoFalse, msoTrue, picLeft, picTop, -1, -1)
    
    'change the picture name
    lookupPicture.Name = "PictureLookup" & Index
    
    PictureLookup = ""
    
    End Function
    

    此时,图片将按原始大小插入Excel中。我希望他们都有0.38x0.38的确切尺寸。我似乎不知道要在脚本中更改哪些属性,以便发生这种情况。

    谢谢你

    1 回复  |  直到 6 年前
        1
  •  2
  •   Vityata    6 年前
    lookupPicture.Height = 38
    lookupPicture.Width = 38
    

    这是如何缩放属性的 Width Height :

    lookupPicture.ScaleWidth 10, msoFalse, msoScaleFromTopLeft
    lookupPicture.ScaleHeight 10, msoFalse, msoScaleFromTopLeft
    

    MSDN Shape.ScaleWidth Method

    按指定的因子缩放形状的宽度。对于图片和OLE对象,可以指示是否要相对于原始大小或当前大小缩放形状。除图片和OLE对象以外的形状总是相对于其当前宽度缩放。