vba 如何使用VBA在Excel中移动图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14399514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move image in Excel using VBA?
提问by meetpd
I want to move image from one location in excel to another using VBA.
我想使用 VBA 将图像从 excel 中的一个位置移动到另一个位置。
How can we do that?
我们怎么做?
回答by JAGAnalyst
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.
您可以通过更改 .Increment... 命令的参数来调整运动的方向和数量,例如为图像设置动画。
回答by AjV Jsy
Another example : move a picture vertically to line up with a specific row
另一个例子:垂直移动图片以与特定行对齐
Sheets(1).Shapes("Picture 1").Top = Sheets(1).Rows(24).Top
回答by Tom Ridd
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
回答by Rajan
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
这是首先将图片插入到 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