簡體   English   中英

在 Excel VBA 中調整圖片大小

[英]Resizing picture in Excel VBA

我有一個 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

這是縮放屬性WidthHeight

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

MSDN Shape.ScaleWidth 方法

按指定的系數縮放形狀的寬度。 對於圖片和 OLE 對象,您可以指明是要相對於原始大小還是當前大小縮放形狀。 圖片和 OLE 對象以外的形狀總是相對於它們的當前寬度進行縮放。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM