Excel

Create Custom Menu to move to a sheet

Ease of Use

Intermediate

Version tested with

2002, 2003 

Submitted by:

parry

Description:

A custom menu that displays a list of all visible sheets within the workbook. Selecting a sheet from the menu will activate the sheet selected. 

Discussion:

The active sheet has a check against it in the menu (via FaceID) which changes as sheets are activated. The menu is also only available in the workbook containing the code so the menu is deleted and re-created as required. This is achieved by using workbook events such as the Workbook_SheetActivate event to delete the recreate the menu when a sheet is activated to ensure the correct sheet is marked with the FaceID. This also caters for when sheets are added/deleted as a new sheet will be activated after these actions thus forcing the menu to be recreated with the updated details. via this event. 

Code:

instructions for use

			

***Place In ThisWorkbook Module*** Private Sub Workbook_Activate() CreateMenu End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) DeleteMenu End Sub Private Sub Workbook_Deactivate() DeleteMenu End Sub Private Sub Workbook_Open() CreateMenu End Sub Private Sub Workbook_SheetActivate(ByVal Sh As Object) DeleteMenu CreateMenu End Sub ***Place In a standard module*** Option Explicit Option Private Module 'prevent menu macros appearing under Tools|Macros Sub CreateMenu() Dim MenuObject As CommandBarPopup, MenuItem As Object Dim SubMenuItem As CommandBarButton, Sh As Worksheet, i As Long ' Make sure the menus aren't duplicated Call DeleteMenu ' Add the top-level menu to the Worksheet CommandBar Set MenuObject = Application.CommandBars(1). _ Controls.Add(Type:=msoControlPopup, Temporary:=True) 'Name of top level menu. Remember to also change caption in DeleteMenu macro MenuObject.Caption = "&My Menu" 'Add 1st menu item Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup) MenuItem.Caption = "Go To Sheet" 'Add sub menu items to 1st menu For Each Sh In ThisWorkbook.Sheets i = i + 1 Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton) SubMenuItem.Caption = Sh.Name SubMenuItem.OnAction = "'LinkSheet(" & i & ")'" If ActiveSheet.Name = Sh.Name Then SubMenuItem.FaceId = 1087 Next Sh End Sub Sub LinkSheet(ShtName As Integer) If IsMissing(ShtName) Then Exit Sub On Error Resume Next Sheets(ShtName).Select Range("A1").Select On Error GoTo 0 End Sub Sub DeleteMenu() ' This sub should be executed when the workbook is closed ' Deletes the Menus On Error Resume Next 'Change &My Menu to the menu name you want Application.CommandBars(1).Controls("&My Menu").Delete On Error GoTo 0 End Sub

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Double Click on ThisWorkbook in the Project Explorer and paste the code from above designated for ThisWorkbook.
  4. Insert | Module.
  5. Paste the code from above designated for a Standard Module.
  6. Close the VBE (Alt + Q or press the X in the top-right corner).
  7. Close and Save the workbook.
 

Test the code:

  1. Open the workbook.
  2. From the Worksheet Menu Bar select My Menu.
  3. Select Goto Sheet from the drop down, then select the sheet to activate.
 

Sample File:

Book1.zip 6.81KB 

Approved by mdmackillop


This entry has been viewed 198 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express