我想使用 VBA 将图像从 excel 中的一个位置移动到另一个位置。
我们怎么能做到这一点?
如果您需要更改图像在给定工作表中的位置,您可以使用以下内容:
ActiveSheet.Shapes.Range(Array("Picture 1")).Select
Selection.ShapeRange.IncrementLeft 100
您可以通过更改 .Increment... 命令的参数来调整运动的方向和数量,例如为图像设置动画。
另一个例子:垂直移动图片以与特定行对齐
Sheets(1).Shapes("Picture 1").Top = Sheets(1).Rows(24).Top
如果我们走得又快又脏,需要在床单之间移动,则以下工作
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
这是首先将图片插入 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