【问题标题】:Resizing picture in Excel VBA在 Excel VBA 中调整图片大小
【发布时间】:2020-07-08 04:45:26
【问题描述】:

我有一个 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 的确切尺寸。我似乎无法弄清楚要在脚本中更改哪些属性才能发生这种情况。

谢谢

【问题讨论】:

    标签: excel vba image


    【解决方案1】:
    lookupPicture.Height = 38
    lookupPicture.Width = 38
    

    这是缩放属性WidthHeight的方法:

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

    MSDN Shape.ScaleWidth Method

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

    【讨论】:

    • 嗨@Vityata,感谢您的快速回复。该方法是否将图像大小更改为 0.38x0.38px 的确切大小,还是将每个图像缩小到各自大小的 38%?谢谢
    • @franciscofcosta - 它可以扩展。但是,通过一些数学运算,如果您知道初始高度是多少,您可以决定它应该缩放多少,以获得 0.38px。
    • @franciscofcosta - 如果你知道如何使用 Python,这是插入精确尺寸图像的好方法 - stackoverflow.com/a/51592397/5448626
    • 谢谢,还是 pyhton 初学者,所以不知道如何在 Python 中完成。我现在会坚持使用 scale 方法。谢谢!
    • @franciscofcosta - 我已编辑,它应该与 .Height.Width 一起使用。
    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    相关资源
    最近更新 更多