在excelvba中调整图片大小

在excelvba中调整图片大小,excel,vba,image,Excel,Vba,Image,我有一个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 Doubl

我有一个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。我似乎无法确定要在脚本中更改哪些属性才能实现这一点

多谢各位

lookupPicture.Height = 38
lookupPicture.Width = 38

这是如何缩放属性
宽度
高度

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


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

Hi@Vityata,感谢您的快速回复。该方法是将图像大小更改为0.38x0.38px的精确大小,还是将每个图像的大小缩小到各自大小的38%?感谢you@franciscofcosta-它可以伸缩。但是,通过一些数学运算,如果你知道初始高度是多少,你可以决定它应该缩放多少,以获得0.38px。@franciscofcosta-如果你知道如何使用Python,这是插入精确大小图像的一个很好的解决方案-谢谢你,仍然是Python初学者,因此不确定如何在Python中执行此操作。我现在还是坚持比例法。谢谢大家!@方济各会-我已经编辑过,它应该与
.Height
.Width
一起使用。