XL2000: Sample Macros for Customizing Menus and Submenus

ID: Q213550


The information in this article applies to:
  • Microsoft Excel 2000


SUMMARY

In Microsoft Excel 2000, toolbars, menu bars, and shortcut menus are considered a single type of object, called a command bar. This behavior is different from versions of Excel earlier than Excel 97. Although most Visual Basic for Applications macros that you create in earlier versions of Excel that customize menus and toolbars work in Excel 2000, some may fail. If this happens, modify the macro code to work with the new object type.

This article provides several examples that illustrate how to customize menu bars, menus, and toolbars in Microsoft Excel 2000.

NOTE: In many of the sample macros in this article, the ID number for a particular control is used as an argument for the Add method. You must know the ID number if you want to restore built-in menus that you deleted.

For a list of ID numbers assigned to built-in command bar controls, please see the following article in the Microsoft Knowledge Base:

Q213552 XL2000: List of ID Numbers for Built-In Command Bar Controls
To find out what ID is assigned to a specific command bar control, please see the following article in the Microsoft Knowledge Base:
Q213211 XL2000: Sample Macros to Return ID for a CommandBar Control


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

Deleting an Entire Menu Bar

The following macro disables the worksheet menu bar. After you run the following macro, the menu bar is not displayed when a worksheet is active.

Sub Disable_Menu_Bar()
    CommandBars("Worksheet Menu Bar").Enabled = False
End Sub 
The following macro re-enables the worksheet menu bar so that it is displayed when a worksheet is active.

Sub Enable_Menu_Bar()
    CommandBars("Worksheet Menu Bar").Enabled = True
End Sub 

Deleting and Restoring a Menu on a Menu Bar

The following macro deletes the Help menu from the worksheet menu bar.

Sub Delete_Help_Menu()
    CommandBars("Worksheet Menu Bar").Controls("Help").Delete
End Sub 
The following macro restores the Help menu to the worksheet menu bar.

Sub Restore_Help_Menu()
    set x = CommandBars("Worksheet Menu Bar")
    x.Reset
End Sub 
NOTE: This macro resets the entire worksheet menu bar back to its default settings. When you run this macro, all customizations that you made to the worksheet menu bar will be lost.

Deleting and Restoring a Menu Command on a Menu

The following macro deletes the Office on the Web command on the Help menu.

Sub Delete_Menu_Item()
    Set x = CommandBars("Worksheet Menu Bar").Controls("Help")
    x.Controls("Office on the Web").Delete
End Sub 
The following macro restores the Office on the Web command on the Help menu.

Sub Restore_Menu_Item()
    Set x = CommandBars("Worksheet Menu Bar").Controls("Help")
    x.Controls.Add Type:=msoControlButton, Id:=3775, before:=2
End Sub 

Deleting and Restoring a Submenu on a Menu

The following macro deletes the Sheet submenu on the Format menu.

   Sub Delete_Submenu()
       Set x = CommandBars("Worksheet Menu Bar").Controls("Format")
       x.Controls("Sheet").Delete
   End Sub 
The following macro restores the Sheet submenu on the Format menu.

Sub Restore_Submenu()
    Set x = CommandBars("Worksheet Menu Bar").Controls("Format")
    x.Controls.Add Type:=msoControlPopup, ID:=30026, before:=4
    x.Reset
End Sub 

Deleting and Restoring a Menu Command on a Submenu

The following macro deletes the Protect Sheet menu command on the Protection submenu (on the Tools menu).

Sub Delete_Item_on_Submenu()
    Set x = CommandBars("Tools").Controls("Protection")
    x.Controls("Protect Sheet...").Delete
End Sub 
The following macro restores the Protect Sheet menu command on the Protection submenu (on the Tools menu).

Sub Restore_Item_on_Submenu()
    Set x = CommandBars("Tools").Controls("Protection")
    x.Controls.Add Type:=msoControlButton, ID:=893, before:=1
End Sub 

Deleting and Restoring a Menu on a Toolbar

The following macro deletes the Draw menu on the Drawing toolbar.

Sub Delete_Menu_on_Toolbar()
    CommandBars("Drawing").Controls("Draw").Delete
End Sub 
The following macro restores the Draw menu on the Drawing toolbar.

Sub Restore_Menu_on_Toolbar()
    Set x = CommandBars("Drawing")
    x.Controls.Add Type:=msoControlPopup, Id:=30013, before:=1
    x.Reset
End Sub 

Deleting and Restoring a Menu Item on a Shortcut Menu

The following macro deletes the Insert Comment menu command on the worksheet cell shortcut menu.

Sub Delete_Shortcut_menu_item()
    CommandBars("Cell").Controls("Insert Comment").Delete
End Sub 
The following macro restores the Insert Comment menu command on the worksheet cell shortcut menu and restores the separator line that the previous macro deleted.

Sub Restore_Shortcut_menu_item()
    Set x = CommandBars("Cell")
    x.Controls.Add Type:=msoControlButton, Id:=2031, before:=8
    Application.ShortcutMenus(xlWorksheetCell).MenuItems.Add _
        Caption:="-", before:=9
End Sub 


REFERENCES

For more information about programmatically customizing command bars, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "Overview of Command Bars" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Additional query words: commandbar menubar XL2000

Keywords : kbprg kbdta kbdtacode xlui KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.