Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete all buttons on a worksheet?

Tags:

excel

vba

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

like image 340
Andrew Perry Avatar asked Feb 15 '16 12:02

Andrew Perry


Video Answer


1 Answers

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

like image 136
XsiSec Avatar answered Sep 29 '22 22:09

XsiSec