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.)

1

4 Answers 4

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

1
  • Activesheet.Buttons.Delete worked very nicely all on its own - many thanks! Commented Feb 15, 2016 at 14:31
3

See the code below:

Sub DeleteAllShapes() 

    ActiveSheet.Shapes.SelectAll

    Selection.Delete

End Sub
1

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

ActiveSheet.Shapes("my_button_name").Delete
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
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.