如何使用 VBA 在 Excel 工作表中的现有形状之后粘贴形状?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10244040/
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 paste a Shape after existing Shapes in Excel Sheet using VBA?
提问by M3HD1
I want to copy a Shape and paste it a Sheet already containing one or more Shapes. I tried using the following simple code :
我想复制一个形状并将其粘贴到一个已经包含一个或多个形状的工作表中。我尝试使用以下简单代码:
myShape.Select
Selection.Copy
ActiveWorkbook.Sheets(mySheet).Paste
But it pastes it above the existing Shapes in the Sheet ...
但它会将它粘贴到工作表中现有的形状之上......
Is there a solution to detect the end of the existing shapes or to paste directly after ? Thx
是否有解决方案可以检测现有形状的结尾或直接粘贴到 之后?谢谢
回答by Siddharth Rout
Is this what you are trying?
这是你正在尝试的吗?
Sub Sample()
Dim myShape As Shape
Set myShape = ActiveSheet.Shapes("Rectangle 1")
myShape.Copy
ActiveSheet.Paste
With Selection
.Top = myShape.Height + 10
.Left = myShape.Left
End With
End Sub
If there are more shapes then you will have to loop through all the shapes and then find the last shape and take that shape's .Top
and .Height
into consideration.
如果有更多的形状,然后你将不得不遍历所有的形状,然后找到最后的形状,并采取形状的.Top
和.Height
考虑。
See this example
看这个例子
Option Explicit
Sub Sample()
Dim myShape As Shape, shp As Shape
Dim sHeight As Double, sTopp As Double
For Each shp In ActiveSheet.Shapes
If shp.Top > sTopp Then
sTopp = shp.Top
sHeight = shp.Height
End If
Next
Set myShape = ActiveSheet.Shapes("Rectangle 1")
myShape.Copy
ActiveSheet.Paste
With Selection
.Top = sTopp + sHeight + 10
.Left = myShape.Left
End With
End Sub