Replace Excel's standard menu using VBA macros
On this page I show how to replace Excel's standard menu with your own. On the page How to make your own menus in Excel with VBA you can see how to add menus to the standard menu, but in this example we replace the menu completely.
NOTE: In Excel 2007 and newer you don't replace Excel's built-in menu, and I don't know if it is possible anymore. Instead you get a new menu item, "AddIns," and if selected, the whole custom made menu is displayed below the standard menu.
It can be relevant to make your own menu, if for instance you have made an application with many possibilities, or if you want better control of the user's actions.
The special menu will only be active in the special spreadsheet. This is ensured by the two last macros on this page ("Private Sub Workbook_Activate" and "Private Sub Workbook_Deactivate"), which must be inserted in the code sheet for "ThisWorkbook". It is quite easy, and I'll explain how later.
In the example we first replace Excel's standard menu with our own "empty" menu, before we build the menu item by item. As the first menu item to the left we make a "discount" version of Excel's Files menu, where you can open, close, save, print and exit. I have done that to show, how you can use Excel's built-in functionality.
After that we add our own menu items. To get any real action you must write the macros that are called by the different menus - in the example I have just made some message boxes that pop up.
To test the code, highlight it with the mouse, copy (CTRL+C) and paste (CTRL+V) into a VBA-module.
Sub MakeMenu()
Dim cbMyBar As CommandBar
Dim cbcMain As CommandBarControl
Dim cbcSub As CommandBarControl
On Error GoTo ErrorHandle
Set cbMyBar = CommandBars.Add(Name:="Custom", _
Position:=msoBarTop, MenuBar:=True, Temporary:=True)
With cbMyBar
.Visible = True
.Protection = msoBarNoMove
End With
Set cbcMain = cbMyBar.Controls.Add(msoControlPopup, Temporary:=True)
With cbcMain
.Caption = "&Files"
.Tag = "Files"
.TooltipText = "Open, Save, Print and Exit"
End With
With cbcMain.Controls.Add(Type:= msoControlButton, Temporary:= True)
.Caption = "&Open..."
.OnAction = "FileOpen"
.Style = msoButtonIconAndCaption
.FaceId = 23
.Visible = True
End With
With cbcMain.Controls.Add(Type:= msoControlButton, Temporary:= True)
.Caption = "&Close"
.OnAction = "CloseBook"
.Style = msoButtonAutomatic
.Visible = True
End With
With cbcMain.Controls.Add(Type:= msoControlButton, Temporary:= True)
.Caption = "&Save"
.OnAction = "Save"
.Style = msoButtonIconAndCaption
.FaceId = 3
.Visible = True
.BeginGroup = True
End With
With cbcMain.Controls.Add(Type:= msoControlButton, Temporary:= True)
.Caption = "S&ave as..."
.OnAction = "SaveAs"
.Style = msoButtonIconAndCaption
.FaceId = 3
.Visible = True
End With
With cbcMain.Controls.Add(Type:= msoControlButton, Temporary:= True)
.Caption = "&Print..."
.OnAction = "PrintBook"
.Style = msoButtonIconAndCaption
.FaceId = 4
.Visible = True
End With
With cbcMain.Controls.Add(Type:= msoControlButton, Temporary:= True)
.Caption = "&Exit"
.OnAction = "StopProgram"
.Style = msoButtonAutomatic
.Visible = True
.BeginGroup = True
End With
Set cbcMain = cbMyBar.Controls. _
Add(Type:=msoControlPopup, Temporary:=True)
With cbcMain
.Caption = "&MyMenu1"
.TooltipText = "Do this and that"
End With
Set cbcSub = cbcMain.Controls. _
Add(Type:=msoControlPopup, Temporary:=True)
With cbcSub
.Caption = "&Lamb chops and more..."
End With
With cbcSub.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "Wool..."
.OnAction = "DummyMacro1"
.Style = msoButtonIconAndCaption
.FaceId = 136
End With
With cbcSub.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "B&ones..."
.OnAction = "DummyMacro2"
.Style = msoButtonIconAndCaption
.FaceId = 136
.Enabled = True
End With
With cbcSub.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "&Horse lasagne..."
.OnAction = "DummyMacro3"
.Style = msoButtonIconAndCaption
.FaceId = 136
End With
With cbcSub.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "R&abbit..."
.OnAction = "DummyMacro1"
.Style = msoButtonIconAndCaption
.FaceId = 136
End With
Set cbcSub = cbcMain.Controls. _
Add(Type:=msoControlPopup, Temporary:=True)
With cbcSub
.Caption = "&Stock..."
End With
With cbcSub.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "&Screws..."
.OnAction = "DummyMacro1"
.Style = msoButtonIconAndCaption
.FaceId = 136
End With
With cbcSub.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "&Nails..."
.OnAction = "DummyMacro1"
.Style = msoButtonIconAndCaption
.FaceId = 136
.Enabled = True
End With
With cbcMain.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "&Calculate calories"
.OnAction = "DummyMacro1"
.Style = msoButtonIconAndCaption
.FaceId = 133
.Visible = True
End With
With cbcMain.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "C&alculate weight loss"
.OnAction = "DummyMacro2"
.Style = msoButtonIconAndCaption
.FaceId = 133
.Visible = True
End With
Set cbcMain = cbMyBar.Controls.Add _
(msoControlPopup, 1, , , True)
With cbcMain
.Caption = "M&yMenu2"
.Tag = "MinMenu2"
.TooltipText = "Heroes && villains"
End With
With cbcMain.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "&Heroes"
.OnAction = "DummyMacro2"
.Style = msoButtonIconAndCaption
.FaceId = 990
End With
With cbcMain.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "&Villains"
.OnAction = "DummyMacro2"
.Style = msoButtonIconAndCaption
.FaceId = 990
End With
With cbcMain.Controls. _
Add(Type:=msoControlButton, Temporary:=True)
.Caption = "&Save heroe"
.OnAction = "DummyMacro2"
.Style = msoButtonIconAndCaption
.FaceId = 3
End With
BeforeExit:
Set cbcMain = Nothing
Set cbcSub = Nothing
Set cbMyBar = Nothing
Exit Sub
ErrorHandle:
MsgBox Err.Description & " Error in MakeMenu", vbCritical + vbOKOnly, "Error"
Resume BeforeExit
End Sub
Sub RemoveMenu()
Dim cbBar As CommandBar
On Error GoTo ErrorHandle
For Each cbBar In Application.CommandBars
If Not cbBar.BuiltIn Then cbBar.Delete
Next
Exit Sub
ErrorHandle:
MsgBox Err.Description & " RemoveMenu", vbOKOnly, "Error"
End Sub
Here are the macros called by the various menus. You should of course make your own.
Sub FileOpen()
Application.Dialogs(xlDialogOpen).Show
End Sub
Sub CloseBook()
ActiveWorkbook.Close
End Sub
Sub Save()
ActiveWorkbook.Save
End Sub
Sub SaveAs()
Application.Dialogs(xlDialogSaveAs).Show
End Sub
Sub PrintBook()
Application.Dialogs(xlDialogPrint).Show
End Sub
Sub StopProgram()
Application.Quit
End Sub
Sub DummyMacro1()
MsgBox "Write your own macro.", vbOKOnly, "Hello Dude"
End Sub
Sub DummyMacro2()
MsgBox "Write some VBA code.", _
vbOKOnly, "You are under arrest!"
End Sub
Sub DummyMacro3()
MsgBox "Send more money.", _
vbOKOnly, "Your daughter"
End Sub
The following two macros must be put in the code sheet for "ThisWorkbook". In the VBA editor you doubleclick the icon "ThisWorkbook" just above the modules in Project Explorer. If it isn't visible press CTRL+R.
The macros will make sure that your menu is only visible, when this spreadsheet is active.
Private Sub Workbook_Activate()
MakeMenu
End Sub
Private Sub Workbook_Deactivate()
RemoveMenu
End Sub
Each menu item has more properties than shown in the example MakeMenu. For instance you can make the menu item visible or invisible by writing .Visible = True or .Visible = False.
This could be conditional:
If something Then
.Visible = False
Else
.Visible = True
end If
There are thousands of the small icons (FaceId), that can be used in menus. At this page is an overview with corresponding numbers.
Related:
|