简体   繁体   English

在 Excel VBA 中调整图片大小

[英]Resizing picture in Excel VBA

I have got a VBA script that will fetch pictures and stick them into a specific cell in Excel, depending on another cell's value.我有一个 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

At the moment, the pictures are inserted into Excel in their original size.目前,图片以其原始大小插入到 Excel 中。 I would like for all of them to have the exact size of 0.38x0.38.我希望它们都具有 0.38x0.38 的确切大小。 I can't seem to figure out which properties to change in the script so that that can happen.我似乎无法弄清楚要在脚本中更改哪些属性才能发生这种情况。

Thank you谢谢

lookupPicture.Height = 38
lookupPicture.Width = 38

This is how to Scale the properties Width and Height :这是缩放属性WidthHeight

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

MSDN Shape.ScaleWidth Method MSDN Shape.ScaleWidth 方法

Scales the width of the shape by a specified factor.按指定的系数缩放形状的宽度。 For pictures and OLE objects, you can indicate whether you want to scale the shape relative to the original or the current size.对于图片和 OLE 对象,您可以指明是要相对于原始大小还是当前大小缩放形状。 Shapes other than pictures and OLE objects are always scaled relative to their current width.图片和 OLE 对象以外的形状总是相对于它们的当前宽度进行缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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