Worksheet Menu Bar Buttons

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
Can anyone provide a script that lists the caption names of all work sheet commandbar buttons, one script to list them on a worksheet and another to list them in a listbox.

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
This code does not do anything iwth the Ribbon controls

Rich (BB code):
Sub ListAllButtonsPopulateListBox()
 
    'Code requires a userForm1 with ListBox1
 
    Dim lX As Long
    Dim lY As Long
    Dim sPrintLine As String
    Dim lNextWriteRow As Long
    Dim aryButtonNames() As Variant
 
    Application.DisplayAlerts = False
    On Error Resume Next
    Worksheets("Document.CmdBarAndBtns").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    Worksheets.Add(before:=Sheets(1)).Name = "Document.CmdBarAndBtns"
 
    lNextWriteRow = 1
 
    For lX = 1 To CommandBars.Count
        For lY = 1 To CommandBars(lX).Controls.Count
            Cells(lNextWriteRow, 1) = CommandBars(lX).Controls(lY).Caption
            'if you want to populate a list box then something line this:
            ReDim Preserve aryButtonNames(1 To lNextWriteRow)
            aryButtonNames(lNextWriteRow) = CommandBars(lX).Controls(lY).Caption
            lNextWriteRow = lNextWriteRow + 1
        Next
    Next
 
    'Next line will remove the & from each of the captions (used to mark shortcut keys)
    Cells.Replace What:="&", Replacement:="", LookAt:=xlPart, SearchOrder:= _
        xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
 
    UserForm1.ListBox1.List = aryButtonNames
 
    UserForm1.Show
 
End Sub
 
Upvote 0
Phil, that's exactly what I wanted.

Now that the button names are listed in the listbox can we search the listbox for the name "Access Scripts" and if found then it will delete that commandbar button.

Sometimes the button Access Scripts is not present therefore a normal delete script bugs out even if I use an on error script, that is why I wanted to list the buttons first and then delete it from there.

If I have to create a new thread for this that is fine.
 
Upvote 0
You could also have the code do the search and delete the command bars if they are found. lX counts backwards in case more then one command bar has the caption you are searching for. Replace the For-Next loops in my previous post with this:

Code:
    bFound = False
    For lX = CommandBars.Count To 1 Step -1
        For lY = 1 To CommandBars(lX).Controls.Count
            If CommandBars(lX).Controls(lY).Caption = "Access Scripts" Then
                bFound = True
                Exit For
            End If
        Next
        If bFound Then
            CommandBars(lX).Delete
            bFound = False
        End If
    Next
 
Upvote 0
Phil,

I modified your code as you advised, however it fails to delete the command bar button, the error says.

method delete of object commanbar failed
run-time error -2147467259

Fails in Excel 2000 and 2010 on line

CommandBars(lX).Delete

Code:
Sub newTEST()
 
    'Code requires a userForm1 with ListBox1
 
    Dim lX As Long
    Dim lY As Long
    Dim sPrintLine As String
    Dim lNextWriteRow As Long
    Dim aryButtonNames() As Variant
 
    Application.DisplayAlerts = False
    On Error Resume Next
    Worksheets("Document.CmdBarAndBtns").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    Worksheets.Add(before:=Sheets(1)).Name = "Document.CmdBarAndBtns"
 
    lNextWriteRow = 1
 
   bFound = False
    For lX = CommandBars.Count To 1 Step -1
        For lY = 1 To CommandBars(lX).Controls.Count
            If CommandBars(lX).Controls(lY).Caption = "Access Scripts" Then
                bFound = True
                Exit For
            End If
        Next
        If bFound Then
           [COLOR=black]CommandBars(lX).Delete  ' FAILS HERE[/COLOR]
            bFound = False
        End If
    Next
 
    'Next line will remove the & from each of the captions (used to mark shortcut keys)
    Cells.Replace What:="&", Replacement:="", LookAt:=xlPart, SearchOrder:= _
        xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
 
    UserForm1.ListBox1.List = aryButtonNames
 
    UserForm1.Show
 
End Sub
 
Upvote 0
Solved it, thanks very much Phil.

Code:
CommandBars(lX).Controls(lY).Delete
 
Upvote 0

Forum statistics

Threads
1,215,556
Messages
6,125,495
Members
449,235
Latest member
Terra0013

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