Delete All Buttons on a Sheet with VBA

hobbes922

New Member
Joined
Jan 17, 2012
Messages
11
Hello,

I have a macro that saves a sheet to another workbook. However, that sheet contains buttons with macros, so when saving to a new workbook, the macros get copied over.

Is there a VBA code to delete all of the buttons on a sheet? I can't name them individually, because for some reason, excel changes the name of the button between the sheet and the new workbook.

Is there a generic code to delete all buttons?

Thanks,
Deb
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Code:
Sub Clear_Buttons()
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
    For i = 1 To 10000
        ActiveWorkbook.sheets(i).Buttons.Delete
    Next i

Test on a COPY of your workbook, but I think this is what you need. I've found that even with 10,000 loops, it runs pretty quick.
 
Upvote 0
Code:
Sub Clear_Buttons()
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
    For i = 1 To 10000
        ActiveWorkbook.sheets(i).Buttons.Delete
    Next i
End Sub

Test on a COPY of your workbook, but I think this is what you need. I've found that even with 10,000 loops, it runs pretty quick.

It just occured to me that you said on a SHEET, not in a whole workbook. If that's the case, try this instead:

Code:
Sub Clear_Buttons()
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
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,357
Members
451,642
Latest member
mirofa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top