Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBA Visual Basic for Applications (Microsoft) FAQ

Excel How To

Adding a group of menu items to other menu bars by shetlandbob
Posted: 24 Jun 05 (Edited 24 Jun 05)

This is an add on to an existing FAQ which explains how to add in your own menu.

Have you ever wanted to add a menu item to say the tools menu?  Or even better still your own group under the tools menu? (this will work for any other menu as well, I've just used the tools as an example)

Well here's how:


Sub addMenu()
  Dim cmdbar As CommandBar
  Dim toolsMenu As CommandBarControl
  Dim myMenu As CommandBarPopup
  Dim subMenu As CommandBarControl
' Point to the Worksheet Menu Bar
  Set cmdbar = Application.CommandBars("Worksheet Menu Bar")

' Point to the Tools menu on the menu bar
  Set toolsMenu = cmdbar.Controls("Tools")

' Create My Menu
  Set myMenu = toolsMenu.Controls.Add(Type:=msoControlPopup)
' Create the sub Menu(s)
  Set subMenu = myMenu.Controls.Add
  With myMenu
    .Caption = "My Menu"
    .BeginGroup = True
    With subMenu
      .Caption = "sub Menu"
      .BeginGroup = True
      .OnAction = "'" & ThisWorkbook.name & "'!myMacro" ' Assign Macro to Menu Item
    End With
  End With

End Sub
Private Sub myMacro()
  MsgBox ("My Sub Menu Command")
End Sub

' How to remove the menu item
Sub removeMenu()

  On Error Resume Next
  Dim cmdbar As CommandBar
  Dim CmdBarMenu As CommandBarControl
  Set cmdbar = Application.CommandBars("Worksheet Menu Bar")
  Set CmdBarMenu = cmdbar.Controls("Tools")
  CmdBarMenu.Controls("My Menu").Delete
End Sub

Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close