Select the product you need help with
How to customize menus and menu bars in ExcelArticle ID: 830502 - View products that this article applies to. On This PageSUMMARYThis article describes how to customize menus and menu bars in Microsoft Excel 2000 and later. This article contains step-by-step instructions and code samples to programmatically manage and customize menu bars, menus, commands,
submenus, and shortcut menus in Microsoft Excel. INTRODUCTIONTo perform many of the common tasks that are associated with
customizing menu bars and menus in Microsoft Excel 2000, in Microsoft Excel
2002, and in Microsoft Office Excel 2003, use the Customize
dialog box. To perform more advanced tasks, or to tailor menu bars and menus
for a custom program, you may want to create Microsoft Visual Basic for
Applications (VBA) codes. For more information about how to use the Customize dialog box, click Microsoft Excel Help on the Help menu, type customize menu bar in the Office Assistant or the Answer Wizard, and then click Search to view the topic. This article can help you learn techniques for writing VBA code for customizing menu bars, menus, menu items, submenus, and shortcut menus. MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. Examples in this articleThe examples in this article use VBA code in Excel 2000, in Excel 2002, and in Excel 2003 to customize menus. To use the example macros, follow these steps:
Command barsIn Microsoft Office, toolbars, menu bars, and shortcut menus are all controlled programmatically as one type of object: command bars. All the following items are represented in VBA by CommandBar objects:
In VBA and in Microsoft Visual Basic, buttons and menu items are represented by CommandBarButton objects. The pop-up controls that display menus and submenus are represented by CommandBarPopup objects. In the following examples, the control that is named "Menu" and the control that is named "Submenu" are both pop-up controls that display a menu and a submenu. Both the menu and the submenu are unique CommandBar objects with their own set of controls. In Microsoft Excel, menu bars and toolbars are referred to as the same programmable object type, the CommandBar object. You use the controls in the CommandBar object to refer to menus, menu items, submenus, and shortcut menus. You use a constant with each control in the Type argument to specify the type of control that you want to use for the menu, the submenu, or the command. Control constantsThe following is a list of the various control constants in Excel 2003 that specify the type of graphical control to use for a particular menu bar control:
**= New in Microsoft Excel 2002 ***=New in Microsoft Office Excel 2003 Menu barsA menu bar is a kind of command bar. A menu bar is the kind of object where you add menus, menu items, and submenus.For more information about how to manage menu bars and menu items in Excel, follow these steps:
Collapse this table
Return an ID for a command bar controlThe following example code returns the ID for the active menu bar:Determine the name of the active menu barThe following example code returns the name for the active menu bar:Save the active state (for built-in or for customized menu bars)You may want to declare the OriginalMenuBar variable a public variable so that a subroutine can use it in another subroutine, such as an Auto_Close subroutine. Declaring and using the variable this way resets the user's previous menu bar to its original state. The following sample macro resets the menu bar:Create a custom command barThe following example code creates a custom command bar that is named My Command Bar:You can also create a custom command bar by using the Temporary:=True argument. The Temporary:=True argument permits the command bars to automatically be reset when you quit Excel. The following code uses the Temporary:=True argument to create a custom command bar: Display a custom command barThe following example creates and displays a custom My Custom Bar menu bar, and then replaces the built-in menu bar:Delete a custom command barThe following example code deletes the custom menu bar that is named Custom 1:Hide a command barThe following example code removes the built-in Chart menu bar from the list of available menu bars:Display a command barThe following example code adds the built-in Chart menu bar from the list of available menu bars:Restore a built-in command barRestoring a menu bar resets the default controls (for both menus and menu items). The following example code restores the built-in Chart menu bar:MenusRestoring a menu bar resets the default controls (for both menus and menu items). The following example code restores the built-in Chart menu bar:Add a custom menu control to a command barThe following example code adds the name of a menu that you add programmatically to the Worksheet menu bar. For example, this code adds the menu name New Menu to the to the Worksheet menu bar.Note You can give this menu any name that you want. Disable a menu control on a command barA menu control that is disabled appears dimmed and is not available on a command bar. The following example disables the New Menu menu:Enable a menu control on a command barThe following example code enables the New Menu menu that you disabled in the "Disable a menu control on a command bar" section:Delete a menu control on a command barThe following code example deletes the New Menu menu that you created in the "Add a custom menu control to a command bar" section from the Worksheet menu bar:Restore a menu control on a command barThe following example code restores the built-in Chart menu bar on the Worksheet menu bar:CommandsThe range of modifications that you can make to a command depends on the control type. Generally, buttons are either enabled or are hidden. Edit boxes, drop-down list boxes, and combo boxes are more versatile in that you can add or delete items from the list. Additionally, you can determine the action that is performed by looking at the value of the items that you selected from the list. You can change the action of any control to a built-in function or to a custom function.The following table lists the most common properties of a control and the methods for changing the state, the action, or the contents of a control: Collapse this table
For more information about menus in Excel 2003 and in Excel 2002, follow these steps:
Add a separator bar to a menu controlThe following example code adds a separator bar before the Worksheet command on the Insert menu:Create a custom command control on a menuThe following example code creates a new command that is named Custom1 on the Tools menu of the Worksheet menu bar, and then runs the Code_Custom1 macro when you click Custom1:Put a check mark next to a command controlThe following example code puts a check mark next to the Custom1 command if it is not selected, and then removes the check mark if the Custom1 command is selected:Disable a command control on a command barThe following example code disables the Custom1 command that you created on the Tools menu in the "Create a custom command control on a menu" section:Enable a command control on a command barThe following example code enables the Custom1 command that you disabled in the "Disable a command control on a command bar" section:Delete a command control on a menuThe following example code deletes the Save command on the File menu:Restore a built-in command control on a menuTo restore a command control on a menu, you must know the identification (ID) number for the control. To determine the ID number, see the "Return an ID for a command bar control" section. The following example deletes and then restores the Save command that you deleted in the "Delete a command control on a menu" section:SubmenusSubmenus appear to the side of the parent menu when you click a command. A command that is a submenu control has a small black arrow that is located at the right end of the command name.Add a submenuThe following example code adds a new submenu that is named NewSub to the Tools menu on the Worksheet menu bar:Add a command to a submenuThe following example code adds a new command that is named SubItem1 to the NewSub submenu, and then it runs the Code_SubItem1 macro when you click SubItem1:Disable a command control on a submenuThe following example code disables the same SubItem command that you created in the "Add a command to a submenu" section :The following example enables the same SubItem command: Delete a command on a submenuThe following example deletes the SubItem1 command that you created on the NewSub submenu in the "Add a command to a submenu" section:Disable a submenu controlThe following example code disables the NewSub submenu that you created on the Tools menu in the "Add a submenu" section:Delete a submenu controlThe following example code deletes the NewSub submenu that you created on the Tools menu in the "Add a submenu" section:Shortcut menu barsA shortcut menu is a floating command bar that appears when the user right-clicks an object. A shortcut menu bar can contain the same control types as a command bar and the controls behave the same as the controls on a command bar. You cannot create or modify shortcut menus from the program's interface in most programs. Therefore, you must create and modify your shortcut menus at run time.For more information about shortcut menus in Excel 2002 and in Excel 2003, follow these steps:
Create a new shortcut menu barThe following example code creates a new shortcut menu bar that is named myShortcutBar:Shortcut menusShortcut menu bars appear when you use the right mouse button to click a specific Excel object. Excel has many shortcut menu bars for which a variety of menus are available. You can also create custom shortcut menu bars and customize the built-in menu bars.Create a command on a shortcut menu barThe following example code creates a new menu command that is named Item1 on the myShortcutBar shortcut menu bar and it runs the Code_Item1 macro when you click Item1:Disable a command control on a shortcut menu barThe following example code disables the Item1 command that you created in the "Create a command on a shortcut menu" section:Delete a command on a shortcut menu barThe following example code deletes the menu command that is named Item1 on the myShortcutBar shortcut menu bar:Delete a shortcut menu barDeleting the shortcut menu bar removes all the items. You cannot restore a deleted custom menu bar. To restore it, you must re-create it and all the menu items and the submenus.The following example code deletes the myShortCutBar shortcut menu bar that you created in the "Create a command on a shortcut menu bar" section: Restore a command on a built-in shortcut menu barThe following example code restores the default commands on the worksheet Cell shortcut menu bar:Submenus on shortcut menusYou can create submenus on shortcut menu bars. Submenus appear to the side of the parent menu when you click a command control. A command that is a submenu control has a small, black arrow that is located to the right of its name.Create a new submenu on a shortcut menu barThe following example adds a new submenu that is named NewSub on the worksheet Cell shortcut menu:Create a command control on a submenu that is located on a shortcut menu barThe following macro adds the SubItem1 command to the submenu NewSub that you created on the Cell shortcut menu, and then runs the Code_SubItem1 macro when you click SubItem1:Disable a submenu item control on a shortcut menuThe following example code disables the SubItem1 command on the NewSub submenu:Delete a submenu item control on a shortcut menuThe following example deletes the SubItem1 command on the NewSub submenu:Disable a submenu control on a shortcut menuThe following example code disables the NewSub submenu on the Cell shortcut menu bar:Delete a submenu control on a shortcut menuThe following example code deletes the NewSub submenu that you created on the Cell shortcut menu bar:Find more informationThe following resources are available to provide more information about how to customize menus and menu bars in Excel.Object BrowserThe Object Browser contains a complete list of all the properties and all the methods for a specific command. To find this information, switch to the Visual Basic Editor (press ALT+F11), click Object Browser on the View menu (or press F2), type the name of the control in the Search box, and then press ENTER or click Search.Microsoft Knowledge BaseThe Microsoft Knowledge Base is a primary Microsoft product information source for Microsoft Product Support Services support professionals. The Microsoft Knowledge Base is also available to Microsoft customers. This comprehensive database contains detailed articles with technical information about Microsoft products, documented fix lists, documentation errors, and answers to frequently asked technical support questions.To connect to the Microsoft Knowledge Base, visit the following Microsoft Web site, and then follow the instructions that appear on the page: http://support.microsoft.com
(http://support.microsoft.com/)
Properties | Article Translations |


Back to the top








