如何使用VBA在Excel中移动图像?

3
我想通过VBA将Excel中的图像从一个位置移动到另一个位置。
我们该如何实现呢?
4个回答

4

如果你需要在给定的工作表中更改图像的位置,可以使用类似以下代码:

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

通过更改 .Increment... 命令的参数,您可以调整运动的方向和数量,例如为图像添加动画效果。


3
另一个例子:将图片垂直移动以与特定行对齐。
Sheets(1).Shapes("Picture 1").Top = Sheets(1).Rows(24).Top

2
如果我们需要快速地在工作表之间移动,可以使用以下方法。
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

0

以下是将图片插入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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接