PDA

View Full Version : Macro Menu



sasa
01-29-2012, 02:31 AM
Hi All,

Is it possible to modify this code so that I can add as many macros as I need.
In other words under the voice "Menu 1", My Macro 1, My Macro 2, My Macro 3,.... and so on. Only a Menu but many macro.
Thank you for any help
sasa


Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim cbcCutomMenu As CommandBarControl

Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup)
cbcCutomMenu.Caption = "&New Menu"

With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Menu 1"
.OnAction = "MyMacro1"
End

With With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Menu 2"
.OnAction = "MyMacro2"
End With
End Sub

marreco
01-29-2012, 04:03 AM
Hi

Welcome
Please use tags when posting code vba

sasa
01-29-2012, 07:13 AM
Hi All,

Is it possible to modify this code so that I can add as many macros as I need.
In other words under the voice "Menu 1", My Macro 1, My Macro 2, My Macro 3,.... and so on. Only a Menu but many macro.
Thank you for any help
sasa


Sub

Code:

AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim cbcCutomMenu As CommandBarControl

Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup)
cbcCutomMenu.Caption = "&New Menu"

With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Menu 1"
.OnAction = "MyMacro1"
End

With With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Menu 2"
.OnAction = "MyMacro2"
End With
End Sub


Local Time: 09:12 AM
Local Date: 01-29-2012
Location: http://www.vbaexpress.com/forum/images/flags/Alderney%203D.gif http://www.vbaexpress.com/forum/images/flags/provinces/Noord-Brabant%203D.gif

http://www.vbaexpress.com/forum/images/buttons/quote.gif (http://www.vbaexpress.com/forum/newreply.php?do=newreply&p=258908)

Paul_Hossler
01-29-2012, 07:28 AM
Actually, at the top of the message window, there's a button labeled VBA

That will insert the special format tags, and then you can paste your code in between


AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim cbcCutomMenu As CommandBarControl

Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar")
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup)
cbcCutomMenu.Caption = "&New Menu"

With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Menu 1"
.OnAction = "MyMacro1"
End

With With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Menu 2"
.OnAction = "MyMacro2"
End With
End Sub


Paul

p45cal
01-29-2012, 08:11 AM
cross-posted here:http://www.mrexcel.com/forum/showthread.php?t=609097

mikerickson
01-29-2012, 11:52 AM
I think this will do what you want.
Note that the End line has been replaced with End With

Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim cbcCutomMenu As CommandBarControl

Set cbMainMenuBar = Application.CommandBars("Custom 1")
With cbMainMenuBar.Controls.Add(Type:=msoControlPopup)
.Caption = "&New Menu"

With .Controls.Add(Type:=msoControlButton)
.Caption = "Menu 1"
.OnAction = "MyMacro1"
End With

With .Controls.Add(Type:=msoControlButton)
.Caption = "Menu 2"
.OnAction = "MyMacro2"
End With
End With
End Sub

sasa
01-29-2012, 02:59 PM
Thanks but it is not so. I mean, only a menù and many macros to start and not one menu, one macro.
:hi:

mikerickson
01-29-2012, 07:34 PM
What do you want
The code that I put adds a control to the toolbar Custom 1 (you can change it back to Worksheet Menu Bar).
Pressing that control will show a menu with two entries to select between the two macros.

What do you want?