I have a set of shapes, which can be deleted.
However if at least one of them is deleted, then I am getting an error, that the object under the specified name wasn’t found.
Basically I would like to make a comment after all, informing me, that all of the shapes have been already deleted.
My code looks like this so far:
Sub Civremov() ActiveSheet.Shapes("Tobyshape").Delete ActiveSheet.Shapes("Toby").Delete ActiveSheet.Shapes("Upturnshape").Delete ActiveSheet.Shapes("Upturndesc").Delete ActiveSheet.Shapes("Duct1").Delete ActiveSheet.Shapes("Duct2").Delete End Sub
Now, when I attempt to delete these elements again, the errors says, that the object under a specified name wasn’t found.
I would like to make a textbox like this:
Msgbox("You have already deleted all civil features").
How can I do this?
You could catch an error, however personally I usually like to loop over shapes, check their properties (name in your case) and act accordingly. You could opt to delete shapes when their name is found in a certain array of names?
Sub Test() Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("YourSheetName") Dim arr As Variant: arr = Array("Tobyshape", "Toby", "Upturnshape", "Upturndesc", "Duct1", "Duct2") For Each shp In ws.Shapes If IsNumeric(Application.Match(shp.Name, arr, 0)) Then shp.Delete End If Next Msgbox "All relevant shapes are deleted." 'Optional End Sub
If we are speaking of a certain type of shape, you could also include that check into your
For loop to minimize the times we call