I use the following VBA to insert a button in each sheet:
Sub Insert_Button_in_each_sheet()
Dim b As Worksheet
For Each b In Worksheets
b.Select
Dim Button_01 As Button
Set Button_01 = b.Buttons.Add(423.75, 0, 48, 15)
Set Range_Button_01 = b.Range("B1:C5")
Selection.Name = "Button_01"
With Button_01
.Top = Range_Button_01.Top
.Left = Range_Button_01.Left
.Width = Range_Button_01.Width
.Height = Range_Button_01.Height
.Text = "Button_01"
End With
Next b
End Sub
All this works fine.
Now, I want to delete this button in each sheet using this VBA:
Sub Delete_Existing_Button()
Dim b As Worksheet
For Each b In Worksheets
b.Select
b.Shapes("Button_01").Delete
Next b
End Sub
However, when I run this code I get runtime error 5.
What do I need to change in my code to make it work?
Button_01.Name = "Button_01"to your Insert code. See if the delete code works thenSelectionName =...never useselectionexcept for a good reason (and they are very rare).selectand.selectionand use explicit references to your objects instead. Furthermore, if you want your code to be consistent, since you used the.buttonscollection in the first sub, you should use it in the second one as well instead of using the.shapescollection. It will work both ways but for the sake of consistency I would use only one of two.