I created this function to go through my current sheet and delete all shapes starting with "Stn_". It seems to only delete a few at a time. I have to end up running it multiple times to delete them all. I'm not sure why it's not working correctly...
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
Would anyone have any pointers or explanations on why it's doing this?
Much appreciated.
CodePudding user response:
you don't need to select you shape and then to delete it just try the following :
Private Sub btnReset_click()
Dim shp As Shape
For Each shp In ActivePage.Shapes
Debug.Print shp.Name
If shp.Name Like "Stn_*" Then
shp.Delete
End If
Next
End Sub
CodePudding user response:
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.