11

I've got a bit of code that creates a Save button on a worksheet (held in the wsReport variable), but it doesn't remove previous buttons. Over time, they tend to build up. Is there any way to do something like this?

wsReport.Buttons.All.Delete

(Not right, obviously, but gives an idea of what I'm looking for.)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Andrew Perry
  • 743
  • 2
  • 11
  • 32
  • Possible duplicate of [Deleting a collections of VBA buttons](https://stackoverflow.com/questions/8898976/deleting-a-collections-of-vba-buttons) – Liam Jul 27 '18 at 14:16

4 Answers4

17

See code below :)

Sub RemoveButtons()
Dim i As Integer
    If ActiveSheet.ProtectContents = True Then
        MsgBox "The Current Workbook or the Worksheets which it contains are protected." & vbLf & "                          Please resolve these issues and try again."
    End If

    On Error Resume Next
        ActiveSheet.Buttons.Delete
End Sub

Source: http://www.mrexcel.com/forum/excel-questions/609668-delete-all-buttons-sheet-visual-basic-applications.html

or could you use code below: (Buttons in VBA are in the Shapes collection).

Sub DelButtons()
Dim btn As Shape

For Each btn In ActiveSheet.Shapes
    If btn.AutoShapeType = msoShapeStyleMixed Then btn.Delete
Next

End Sub

source: Deleting a collections of VBA buttons

Community
  • 1
  • 1
XsiSecOfficial
  • 954
  • 8
  • 20
3

See the code below:

Sub DeleteAllShapes() 

    ActiveSheet.Shapes.SelectAll

    Selection.Delete

End Sub
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Info City
  • 31
  • 1
1

If somebody found this question, but needs to delete only one button, this helped me:

ActiveSheet.Shapes("my_button_name").Delete
elano7
  • 1,584
  • 1
  • 18
  • 18
0

There's an easier method that doesn't use VB:

  1. Click the Developer tab
  2. Click Design Mode
  3. Click on any button in the worksheet
  4. Press Ctrl + A to select all buttons in the worksheet
  5. Press Delete
thatguy
  • 47
  • You need to activate Developer Tab first: [Display the Developer tab in the toolbar](https://www.techonthenet.com/excel/questions/developer_tab2013.php) – Ali Şentürk May 23 '20 at 00:29