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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,223,594
Messages
6,173,243
Members
452,505
Latest member
Neeraj1990

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