MenuBar Object

Description

Represents a built-in or custom menu bar.

Accessors

The MenuBar object is a member of the MenuBars collection. The MenuBars collection contains all of the menu bars available to Microsoft Excel. Use the Add method to create a new menu bar and add it to the collection. Use the Activate method to display a menu bar. To access a single member of the collection, use the MenuBars method with the menu bar caption or index number as an argument.

Several built-in constants are available for the menu bar index number, as shown in the following list.

Constant

Description

xlWorksheet

Worksheet, macro sheet, and dialog sheet.

xlChart

Chart

xlModule

Visual Basic module

xlNoDocuments

No documents open

xlInfo

Info Window

xlWorksheetShort

Short Worksheet menu (for Microsoft Excel version 3 compatibility)

xlChartShort

Short Chart menu (for Microsoft Excel version 3 compatibility)

xlWorksheet4

Old worksheet menu bar (for Microsoft Excel version 4 compatibility)

xlChart4

Old chart menu bar (for Microsoft Excel version 4 compatibility)


Microsoft Excel automatically displays the built-in menu bar appropriate to the active sheet. When the active sheet type changes, Microsoft Excel changes the menu bar. If you create a custom menu bar and display it using Visual Basic code, Microsoft Excel stops changing the menu bar to fit the active sheet, and your code must activate and deactivate the menu bar (if necessary) when the sheet type changes.

The following example adds a new menu item to the bottom of the File menu on the Visual Basic Module menu bar.


MenuBars(xlModule).Menus("file").MenuItems.Add "S&earch"

The following example creates a table on worksheet one. The table contains the captions of all the menus on all the menu bars in the application. The column headings are the menu bar captions, and the column entries under each heading are the menu captions on that menu bar.


Sub EnumerateMenuBars()
    Worksheets(1).Activate
    c = 1
    For Each mb In MenuBars
        Cells(1, c) = mb.Caption
        i = 2
        For Each mn In mb.Menus
            Cells(i, c) = mn.Caption
            i = i + 1
        Next
        c = c + 1
    Next
End Sub

Properties

Application Property, BuiltIn Property, Caption Property, Creator Property, Index Property, Parent Property.

Methods

Activate Method, Delete Method, Menus Method, Reset Method.