Also this will add a new item to the default Excel bar and than add items to its dropdown, just like if you clicked on the first item of the default bar "File" which than drops down that items options:
Sub myAdd_MyMenu_ToDefaultToolbar()
'Standard module code, like: Module1!
Dim myNewMainMenu, myNewMainMenuItem
'This adds a new item to the Default toolbar at the very top of the page, like: File Edit View...
On Error GoTo myErr
'Delete custom menu if it exists!
Call Remove_MyMenu
'Add a new item to the default toolbar!
With CommandBars("Worksheet Menu Bar")
Set myNewMainMenu = .Controls.Add(Type:=msoControlPopup, temporary:=True)
End With
'Name this new toolbar item!
myNewMainMenu.Caption = "MyMenu"
'Add a sub-menu item to the new menu list!
Set myNewItem1 = myNewMainMenu.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
With myNewItem1
.Caption = "Un-Install"
.TooltipText = "Un-install this MyMenu from this toolbar!"
.Style = msoButtonCaption
.OnAction = "Remove_MyMenu" 'Name of macro to run.
End With
'Add a Second sub-menu item to the new menu list!
Set myNewItem2 = myNewMainMenu.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
With myNewItem2
.Caption = "Run Test"
.TooltipText = "Runs the macro for this item!"
.Style = msoButtonCaption
.OnAction = "myTestItem" 'Name of macro to run.
End With
Exit Sub
myErr:
MsgBox "An error has occured, did not create menu items. " & Chr(13) & _
"Error number: " & Err.Number & Chr(13) & "Error Description: " & Err.Description, vbExclamation + vbOKOnly, "Error!"
Resume Next
End Sub
Sub Remove_MyMenu()
'Standard module code, like: Module1!
'Removes custom menu if it exists!
On Error Resume Next
CommandBars("Worksheet Menu Bar").Controls("MyMenu").Delete
On Error GoTo 0
End Sub
Private Sub myTestItem()
'Standard module code, like: Module1!
MsgBox "This is a test for a Sub-Menu item activation!"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'ThisWorkbook module code, Only!
'Removes custom menu if it exists!
Call Remove_My_Menu
End Sub
Private Sub Workbook_Open()
'ThisWorkbook module code, Only!
'Adds custom menu to this workbook.
Call myAdd_MyMenu_ToDefaultToolbar
End Sub
Private Sub Workbook_Deactivate()
'ThisWorkbook module code, Only!
'Removes custom menu if it exists!
Call Remove_MyMenu
End Sub
Private Sub Workbook_Activate()
'ThisWorkbook module code, Only!
'Adds custom menu to this workbook.
Call myAdd_MyMenu_ToDefaultToolbar
End Sub