简体   繁体   中英

How to move image in Excel using VBA?

I want to move image from one location in excel to another using VBA.

How can we do that?

If you need to change the position of the image within a given worksheet, you can use something like this:

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

You can adjust the direction and amount of motion by changing the parameters of the .Increment... command, to animate an image for example.

If we are going quick and dirty and need to move between sheets the following works

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

Here is the code to first insert picture to Excel and then adjust or resize the Picture. Later move the Picture towards down or right

'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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM