Excel Developer Tip


Return to The Spreadsheet Page

Excel page

Tip archives

Creating Custom Menus

If you've moved up to Excel 97 (or later) from a previous version, you may have noticed that the former menu editor is no longer available. The menu editor made it easy to create a custom menu that was stored with a particular workbook. Opening the workbook added the menu, and closing the workbook removed the menu.

The menu editor was removed because Excel 97 and later versions use CommandBars - a new (and much superior) method of dealing with menus. Workbook-specific menus must be created programmatically using VBA code. This tip describes a relatively simple way to create a custom menu (on the Worksheet Menu Bar) that appears when a particular workbook is opened, and is deleted when the workbook is closed.

Download an example

You can download an example workbook that demonstrates this technique.

The example file contains all of the VBA code that you need to create your own custom menus. In most cases, you will not need to make any changes to the VBA code - simply customize the MenuSheet worksheet.

Please note that this technique will not work if you need to add a menu item to an existing menu.

How it works

This technique uses a table, which is stored in a worksheet. The figure below shows such a table. To create a custom menu, simply modify the data in the table.

This table contains five columns:

  • Level: The "level" of the particular item. Valid values are 1, 2, and 3. A level of 1 is for a menu; 2 is for a menu item; and 3 is for a submenu item. Normally, you'll have one level 1 item, with level 2 items below it. A level 2 item may or may not have level 3 (submenus) items.
  • Caption: The text that appears in the menu, menu item, or submenu. Use an ampersand (&) to specify a character that will be underlined.
  • Position/Macro: For level 1 items, this should be an integer that represents the position in the menubar. For level 2 or level 3 items, this will be the macro that is executed when the item is selected. If a level 2 item has one or more level 3 items, the level 2 item may not have a macro associated with it.
  • Divider: True if a "divider" should be placed before the menu item or submenu item.
  • FaceID: Optional. A code number that represents the built-in graphic images that are displayed next to an item. Follow this link for more information about determining FaceID code numbers.

An example menu

The figure below shows the menu that is created using the table above.

Using this technique

To use this technique in your workbook or add-in, follow these general steps:

  1. Download menumakr.xls. This file contains the VBA code, plus a worksheet name MenuSheet.

  2. Copy all of the code in Module1 to a module in your project.

  3. Add subroutines like these to the code module for the ThisWorkbook object:

    Private Sub Workbook_Open()
        Call CreateMenu
    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Call DeleteMenu
    End Sub

  4. The Workbook_Open subroutine is executed when the workbook is opened; the Workbook_BeforeClose subroutine is executed before the workbook is closed.

  5. Insert a new worksheet and name it MenuSheet. Better yet, copy the MenuSheet from the menumakr.xls file.

  6. Customize the MenuSheet to correspond to your custom menu.

  7. There is no error handling, so it's up to you to make sure that everything works.