Robit010
Robit010

Reputation: 63

Deleting all Like shapes on active sheet not deleting all

I created this function to go through my current sheet and delete all shapes starting with "Stn_".

It deletes a few at a time. I end up running it multiple times to delete them all.

Private Sub btnReset_click()
'Reset Shapes needs work

For Each shp In ActivePage.Shapes
    Debug.Print shp.Name
    If shp.Name Like "Stn_*" Then
        ActiveWindow.Select shp, visSelect
        ActiveWindow.Selection.Delete
    End If
Next
End Sub

Upvotes: 0

Views: 472

Answers (1)

Paul Herber
Paul Herber

Reputation: 1200

You need to count backwards when deleting shapes. If you, say, delete shape 1, then shape 2 then becomes the new shape 1 but the loop counter moves on to 2, bypassing that shape. So you can't use For Each, you have to use a plain old For but count by -1.

Upvotes: 2

Related Questions