This uses my original code and now adds 5 menu items, so you can see how to do it. I tested it and it works for me!
This code must go into a Standard code module like: Module1!
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 Test1"
.TooltipText = "Runs the macro for this item!"
.Style = msoButtonCaption
.OnAction = "myTestItem" 'Name of macro to run.
End With
'Add a Third sub-menu item to the new menu list!
Set myNewItem3 = myNewMainMenu.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
With myNewItem3
.Caption = "Run Test2"
.TooltipText = "Runs the macro for this item!"
.Style = msoButtonCaption
.OnAction = "myTestItem" 'Name of macro to run.
End With
'Add a Fourth sub-menu item to the new menu list!
Set myNewItem4 = myNewMainMenu.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
With myNewItem4
.Caption = "Run Test3"
.TooltipText = "Runs the macro for this item!"
.Style = msoButtonCaption
.OnAction = "myTestItem" 'Name of macro to run.
End With
'Add a Fith sub-menu item to the new menu list!
Set myNewItem5 = myNewMainMenu.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
With myNewItem5
.Caption = "Run Test4"
.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
This next code can only go into the "ThisWorkbook" code module!
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'ThisWorkbook module code, Only!
'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 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
If you paste the code correctly it should be automatic.
To get it to work save it close it and re-open it and all the code will be set from then on it will work automatically!