vba to delete buttons in a worksheet with criteria

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
679
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have the code below that deletes all buttons throughout a workbook.
I would like to be able to adapt the code to do the following:
(a) delete all buttons in the workbook except any buttons found in the worksheet named "Export Tabs"
(b) delete all buttons in the workbook except any buttons found that are named "Export1" and 'Export2" in any worksheet
These are independent solutions, so would like two separate codes if possible.
Hope you can help, many thanks.
VBA Code:
For Each b In Worksheets
        
    b.Buttons.Delete
    
    Next b
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this.

Separated:
VBA Code:
Sub deleteButtonsConditional()

    For Each sht In Worksheets
        For Each btn In sht.Buttons
            If sht.Name <> "Export Tabs" Then
            btn.Delete
            End If
        Next
    Next

End Sub
VBA Code:
Sub deleteButtonsConditional()

    For Each sht In Worksheets
        For Each btn In sht.Buttons
            If sht.Buttons(btn.Name).Name <> "Export1" And sht.Buttons(btn.Name).Name <> "Export2" Then
            btn.Delete
            End If
        Next
    Next

End Sub

Combined:
VBA Code:
Sub deleteButtonsConditional()

    For Each sht In Worksheets
        For Each btn In sht.Buttons
            If sht.Name <> "Export Tabs" And sht.Buttons(btn.Name).Name <> "Export1" And sht.Buttons(btn.Name).Name <> "Export2" Then
            btn.Delete
            End If
        Next
    Next

End Sub
 
Upvote 0
Solution
These are independent solutions, so would like two separate codes if possible.

Should only need one code to perform required operation

Place in standard module

VBA Code:
Sub DeleteAllButtonsExcept(ParamArray KeepButtons() As Variant)
    Dim sh   As Worksheet
    Dim shp  As Shape

    For Each sh In ThisWorkbook.Worksheets
        For Each shp In sh.Shapes
           m = Application.Match(shp.Name, Array(KeepButtons), 0)
           If IsError(m) Then shp.Delete
        Next shp
    Next sh
   
End Sub

Then from your code, call the sub passing to it the button name(s) you want to keep

VBA Code:
    DeleteAllButtonsExcept "Export Tabs"
   
OR

    DeleteAllButtonsExcept "Export1", "Export2"

OR
   
    DeleteAllButtonsExcept "Export Tabs", "Export1", "Export2"

The buttons names argument list can be as many as required
Solution not fully tested but hopefully, will do what you want

Hope Helpful

Dave
 
Upvote 0
Try this.

Separated:
VBA Code:
Sub deleteButtonsConditional()

    For Each sht In Worksheets
        For Each btn In sht.Buttons
            If sht.Name <> "Export Tabs" Then
            btn.Delete
            End If
        Next
    Next

End Sub
VBA Code:
Sub deleteButtonsConditional()

    For Each sht In Worksheets
        For Each btn In sht.Buttons
            If sht.Buttons(btn.Name).Name <> "Export1" And sht.Buttons(btn.Name).Name <> "Export2" Then
            btn.Delete
            End If
        Next
    Next

End Sub

Combined:
VBA Code:
Sub deleteButtonsConditional()

    For Each sht In Worksheets
        For Each btn In sht.Buttons
            If sht.Name <> "Export Tabs" And sht.Buttons(btn.Name).Name <> "Export1" And sht.Buttons(btn.Name).Name <> "Export2" Then
            btn.Delete
            End If
        Next
    Next

End Sub
they work perfectly, thank you very much for your time and help with this :)
 
Upvote 0
Should only need one code to perform required operation

Place in standard module

VBA Code:
Sub DeleteAllButtonsExcept(ParamArray KeepButtons() As Variant)
    Dim sh   As Worksheet
    Dim shp  As Shape

    For Each sh In ThisWorkbook.Worksheets
        For Each shp In sh.Shapes
           m = Application.Match(shp.Name, Array(KeepButtons), 0)
           If IsError(m) Then shp.Delete
        Next shp
    Next sh
  
End Sub

Then from your code, call the sub passing to it the button name(s) you want to keep

VBA Code:
    DeleteAllButtonsExcept "Export Tabs"
  
OR

    DeleteAllButtonsExcept "Export1", "Export2"

OR
  
    DeleteAllButtonsExcept "Export Tabs", "Export1", "Export2"

The buttons names argument list can be as many as required
Solution not fully tested but hopefully, will do what you want

Hope Helpful

Dave
ok thanks for this I will try this solution also :)
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,892
Members
449,058
Latest member
Guy Boot

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