I cannot figure out why this code does not "detect" the activex button (created with Siddarth's help in here: How to rename a newly created ActiveX button?). I see the button in front of me and its properties window looks like this.
I even tried to delete just one button with ActiveSheet.Shapes("CommandButton1").Delete
, but that line results in an error window with a text saying something like "An item with that name wasn't found.". => Apparently I am not using the right name to address the button.
The code I am using is based on used Gareth's code in Word vba delete button not working
I added an if-condition to exclude "good" buttons with names set in snake case (hence "_"), i. e. the names were defined by me.
Sub del_button()
Dim obj As Object
For Each obj In ActiveSheet.Shapes
If InStr(obj.OLEFormat.Object.Name, "CommandButton") > 0 And InStr(obj.OLEFormat.Object.Name, "_") < 1 Then
'MsgBox (obj.OLEFormat.Object.Name) - just was there for me to "notice", so I am set to notice when a button is going to be deleted
obj.Delete
End If
Next obj
'ActiveSheet.Shapes("CommandButton1").Delete ---- THIS LINE throws an error
End Sub
=> What name should I specify in the code then, if the one I used isn't correct? Or do I have to use something else entirely?