How to paste a Shape after existing Shapes in Excel Sheet using VBA?

18,180

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.

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
Share:
18,180
M3HD1
Author by

M3HD1

Full Stack Software Engineer. Working on a Hyper-personalization AI platform.

Updated on June 05, 2022

Comments

  • M3HD1
    M3HD1 almost 2 years

    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