簡體   English   中英

如何使用VBA在Excel中移動圖像?

[英]How to move image in Excel using VBA?

我想使用VBA將圖像從excel中的一個位置移動到另一個位置。

我們該怎么做?

如果需要在給定的工作表中更改圖像的位置,則可以使用以下方法:

ActiveSheet.Shapes.Range(Array("Picture 1")).Select
Selection.ShapeRange.IncrementLeft 100

您可以通過更改.Increment ...命令的參數來調整運動的方向和量,例如對圖像進行動畫處理。

如果我們又快又臟又需要在圖紙之間移動,請執行以下操作

Sub CutAndPasteAPicture(shapeName As String, fromSheet As String, toSheet As String, toRange As String)
'Cut and Paste
Sheets(fromSheet).Shapes(shapeName).Cut
Sheets(toSheet).Paste Sheets(toSheet).Range(toRange)
End Sub

Sub Example()
  CutAndPasteAPicture "Picture 1", "Sheet1", "Sheet2", "D2"
End Sub

另一個示例:垂直移動圖片以與特定行對齊

Sheets(1).Shapes("Picture 1").Top = Sheets(1).Rows(24).Top

這是首先將圖片插入Excel,然后調整圖片或調整圖片大小的代碼。 稍后將圖片向下或向右移動

'Insert the Picture from the path if its not present already

Set myPict = Thisworkbook.sheets(1).Range("A1:B5").Parent.Pictures.Insert(ThisWorkbook.Path & "\" & "mypic.jpg")

'Adjust the Picture location
    myPict.Top = .Top
    myPict.Width = .Width
    myPict.Height = .Height
    myPict.Left = .Left
    myPict.Placement = xlMoveAndSize
    'myPict.LockAspectRatio = msoTriStateMixed

'Change the width of the Picture
    myPict.Width = 85
'change the Height of the Picutre
    myPict.Height = 85
End With

'Select the Picutre
myPict.Select

'Move down the picture to 3 points. Negative value move up

Selection.ShapeRange.IncrementTop 3


'Move towards right upto 5 points. Negative value moves towards right
Selection.ShapeRange.IncrementLeft 5

暫無
暫無

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

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