excel menubar contrl vba microsoft

15
1. Start Excel. 2. On the Tools menu, point to Macro, and then click Visual Basic Editor. 3. On the Insert menu, click Module. 4. Type one of the macro examples from this article, or you can use a copy-and-paste operation to copy the macro examples to a module sheet. 5. On the Run menu, click Run Sub/User Form. 6. If the Macro dialog box appears, click the macro name, and then click Run. Article ID: 830502 - Last Review: January 11, 2006 - Revision: 4.3 How to customize menus and menu bars in Excel This 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. To 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. Microsoft 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 article The 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: Page 1 of 15 How to customize menus and menu bars in Excel 25/05/2010 http://support.microsoft.com/kb/830502

Upload: govindarul

Post on 08-Oct-2014

71 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Excel Menubar Contrl VBA MicroSoft

1. Start Excel.

2. On the Tools menu, point to Macro, and then click Visual Basic

Editor.

3. On the Insert menu, click Module.

4. Type one of the macro examples from this article, or you can use a

copy-and-paste operation to copy the macro examples to a module

sheet.

5. On the Run menu, click Run Sub/User Form.

6. If the Macro dialog box appears, click the macro name, and then click

Run.

Article ID: 830502 - Last Review: January 11, 2006 - Revision: 4.3

How to customize menus and menu bars in Excel

This 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.

To 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.

Microsoft 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 article

The 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:

Page 1 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 2: Excel Menubar Contrl VBA MicroSoft

� Menu bars, toolbars, and shortcut menus.

� Menus on menu bars and toolbars.

� Submenus on menus, submenus, and shortcut menus.

� MsoControlActiveX*

� MsoControlAutoCompleteCombo***

� MsoControlButton

� MsoControlButtonDropdown

� MsoControlButtonPopup

� MsoControlComboBox

� MsoControlCustom

� MsoControlDropdown

� MsoControlEdit

� MsoControlExpandingGrid

� MsoControlGauge

� MsoControlGenericDropdown

� MsoControlGraphicCombo

� MsoControlGraphicDropdown

� MsoControlGraphicPopup

� MsoControlGrid

� MsoControlLabel

� MsoControlLabelEx***

� MsoControlOCXDropDown

� MsoControlPane **

� MsoControlPopup

Command bars

In 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:

You can modify any built-in menu bar or any built-in toolbar, and you can

create and modify custom toolbars, menu bars, and shortcut menus to deliver

with your VBA code. You present the features of your program as individual

buttons on toolbars or as groups of command names on menus. Because

toolbars and menus are both command bars, you use the same kind of controls.

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 constants

The 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:

Page 2 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 3: Excel Menubar Contrl VBA MicroSoft

� MsoControlSpinner***

� MsoControlSplitButtonMRUPopup

� MsoControlSplitButtonPopup

� MsoControlSplitDropdown

� MsoControlSplitExpandingGrid

� MsoControlWorkPane**

1. Start the Microsoft Visual Basic Editor.

2. On the Help menu, click Microsoft Visual Basic Help.

3. In the Office Assistant box or in the Answer Wizard box, type Menu

bars, and then click Search.

4. In Excel 2003 and in Excel 2002, click Adding and Managing Menu

Bars and Menu Items. In Excel 2000, click About menus and

toolbars.

*=New in Microsoft Excel 2000

**= New in Microsoft Excel 2002

***=New in Microsoft Office Excel 2003

Menu bars

A 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:

You can make modifications to both the menu bar and to the controls on that

menu bar at run time. The changes that you make to the menu bar may affect

the appearance or the position of the menu bar. Changes that you make to the

controls depend on the control type. The following table lists the most common

properties and the common methods for changing the state, the action, or the

contents of a control:

Return an ID for a command bar control

The following example code returns the ID for the active menu bar:

Page 3 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 4: Excel Menubar Contrl VBA MicroSoft

Sub Id_Control ()

Dim myId as Object

set myId = CommandBars("Worksheet Menu Bar").Controls("Tools")

MsgBox myId.Caption & Chr(13) & MyId.Id

End Sub

Sub MenuBars_GetName()

MsgBox CommandBars.ActiveMenuBar.Name

End Sub

Public OriginalMenuBar as Object

Sub MenuBars_Capture()

Set OriginalMenuBar = CommandBars.ActiveMenuBar

End Sub

Sub MenuBar_Create() Application.CommandBars.Add Name:="My command

bar" End Sub

Sub MenuBar_Create()

Application.CommandBars.Add Name:="My command bar", Temporary:=True

End Sub

Determine the name of the active menu bar

The 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 bar

The 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 bar

The following example creates and displays a custom My Custom Bar menu

bar, and then replaces the built-in menu bar:

Page 4 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 5: Excel Menubar Contrl VBA MicroSoft

Sub MenuBar_Show()

Dim myNewBar As Object

Set myNewBar = CommandBars.Add(Name:="Custom1",

Position:=msoBarFloating)

' You must first enable your custom menu bar before you make it

visible.

' Enabling a menu bar adds it to the list of available menu bars on

' the Customize dialog box.

' Setting the menubar property to True replaces the built-in menu bar.

myNewBar.Enabled = True

myNewBar.Visible = True

End Sub

Sub MenuBar_Delete()

CommandBars("Custom1").Delete

End Sub

Sub MenuBar_Display()

CommandBars("Chart").Enabled = False

End Sub

Sub MenuBar_Display()

CommandBars("Chart").Enabled = True

End Sub

Sub MenuBar_Restore()

CommandBars("Chart").Reset

End Sub

Delete a custom command bar

The following example code deletes the custom menu bar that is named

Custom 1:

Hide a command bar

The following example code removes the built-in Chart menu bar from the list

of available menu bars:

Display a command bar

The following example code adds the built-in Chart menu bar from the list of

available menu bars:

Restore a built-in command bar

Restoring a menu bar resets the default controls (for both menus and menu

items). The following example code restores the built-in Chart menu bar:

Note You can only reset built-in menu bars. You cannot reset a custom menu

bar.

Menus

Page 5 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 6: Excel Menubar Contrl VBA MicroSoft

Sub MenuBar_Restore()

CommandBars("Chart").Reset

End Sub

Sub Menu_Create()

Dim myMnu As Object

Set myMnu = CommandBars("Worksheet menu bar").Controls. _

Add(Type:=msoControlPopup, before:=3)

With myMnu

' The "&" denotes a shortcut key assignment (Alt+M in this case).

.Caption = "New &Menu"

End With

End Sub

Sub Menu_Disable()

CommandBars("Worksheet menu bar").Controls("New &Menu").Enabled =

False

End Sub

Sub Menu_Disable()

CommandBars("Worksheet menu bar").Controls("New &Menu").Enabled =

True

End Sub

Restoring a menu bar resets the default controls (for both menus and menu

items). The following example code restores the built-in Chart menu bar:

Note You can only reset built-in menu bars. You cannot reset a custom menu

bar.

Add a custom menu control to a command bar

The 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 bar

A 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 bar

The 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 bar

The 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:

Page 6 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 7: Excel Menubar Contrl VBA MicroSoft

Sub Menu_Delete()

CommandBars("Worksheet menu bar").Controls("New &Menu").Delete

End Sub

Sub Menu_Restore()

Dim myMnu As Object

Set myMnu = CommandBars("Chart")

myMnu.Reset

End Sub

1. Start the Visual Basic Script Editor.

2. On the Help menu, click Microsoft Visual Basic Help.

3. In the Search Help box, type menus, and then press ENTER.

4. Click Adding and Managing Menu Bars and Menu Items (Office).

Restore a menu control on a command bar

The following example code restores the built-in Chart menu bar on the

Worksheet menu bar:

Commands

The 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:

For more information about menus in Excel 2003 and in Excel 2002, follow

these steps:

Add a separator bar to a menu control

The following example code adds a separator bar before the Worksheet

command on the Insert menu:

Page 7 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 8: Excel Menubar Contrl VBA MicroSoft

Sub menuItem_AddSeparator()

CommandBars("Worksheet menu bar").Controls("Insert") _

.Controls("Worksheet").BeginGroup = True

End Sub

Sub menuItem_Create()

With CommandBars("Worksheet menu bar").Controls("Tools")

.Controls.Add(Type:=msoControlButton, Before:=1).Caption

= "Custom1"

.Controls("Custom1").OnAction = "Code_Custom1"

End With

End Sub

Sub menuItem_checkMark()

Dim myPopup as Object

Set myPopup = CommandBars("Worksheet menu bar").Controls("Tools")

If myPopup.Controls("Custom1").State = msoButtonDown Then

' Remove check mark next to menu item.

myPopup.Controls("Custom1").State = msoButtonUp

MsgBox "Custom1 is now unchecked"

Else

' Add check mark next to menu item.

myPopup.Controls("Custom1").State = msoButtonDown

MsgBox "Custom1 is now checked"

End If

End Sub

Sub MenuItem_Disable()

Dim myCmd as Object

Set myCmd = CommandBars("Worksheet menu bar").Controls("Tools")

myCmd.Controls("Custom1").Enabled = False

End Sub

Note To remove a separator bar, set the BeginGroup property to False.

Create a custom command control on a menu

The 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 control

The 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 bar

The 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:

Page 8 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 9: Excel Menubar Contrl VBA MicroSoft

Sub MenuItem_Enable()

Dim myCmd as Object

Set myCmd = CommandBars("Worksheet menu bar").Controls("Tools")

myCmd.Controls("Custom1").Enabled = True

End Sub

Sub menuItem_Delete()

Dim myCmd As Object

Set myCmd = CommandBars("Worksheet menu bar").Controls("File")

myCmd.Controls("Save").Delete

End Sub

Sub menuItem_Restore()

Dim myCmd As Object

Set myCmd = CommandBars("Worksheet menu bar").Controls("File")

' Id 3 refers to the Save menu item control.

myCmd.Controls.Add Type:=msoControlButton, ID:=3, Before:=5

End Sub

Sub SubMenu_Create()

Dim newSub as Object

Set newSub = CommandBars("Worksheet menu bar").Controls("Tools")

With newSub

.Controls.Add(Type:=msoControlPopup, Before:=1).Caption="NewSub"

End With

End Sub

Enable a command control on a command bar

The 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 menu

The following example code deletes the Save command on the File menu:

Restore a built-in command control on a menu

To 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:

Submenus

Submenus 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 submenu

The following example code adds a new submenu that is named NewSub to

the Tools menu on the Worksheet menu bar:

Page 9 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 10: Excel Menubar Contrl VBA MicroSoft

Sub SubMenu_AddItem()

Dim newSubItem as Object

Set newSubItem = CommandBars("Worksheet menu bar") _

.Controls("Tools").Controls("NewSub")

With newSubItem

.Controls.Add(Type:=msoControlButton, Before:=1).Caption

= "SubItem1"

.Controls("SubItem1").OnAction = "Code_SubItem1"

End With

End Sub

Sub SubMenu_DisableItem()

CommandBars("Worksheet menu bar").Controls("Tools") _

.Controls("NewSub").Controls("SubItem1").Enabled = False

End Sub

Sub SubMenu_DisableItem()

CommandBars("Worksheet menu bar").Controls("Tools") _

.Controls("NewSub").Controls("SubItem1").Enabled = True

End Sub

Sub SubMenu_DeleteItem()

CommandBars("Worksheet menu bar").Controls("Tools") _

.Controls("NewSub").Controls("SubItem1").Delete

End Sub

Add a command to a submenu

The 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 submenu

The 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 submenu

The 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 control

The following example code disables the NewSub submenu that you created on

the Tools menu in the "Add a submenu" section:

Page 10 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 11: Excel Menubar Contrl VBA MicroSoft

Sub SubMenu_DisableSub()

CommandBars("Worksheet menu bar").Controls("Tools") _

.Controls("NewSub").Enabled = False

End Sub

Sub SubMenu_DeleteSub()

CommandBars("Worksheet menu bar").Controls("Tools") _

.Controls("NewSub").Delete

End Sub

1. Start the Visual Basic Script Editor.

2. On the Help menu, click Microsoft Visual Basic Help.

3. In the Search Help box, type shortcut, and then press ENTER.

4. Click Adding and Displaying Shortcut Menus.

Sub Shortcut_Create()

Dim myShtCtBar as Object

Set myShtCtBar = CommandBars.Add(Name:="myShortcutBar", _

Position:=msoBarPopup)

‘ This displays the shortcut menu bar.

‘ 200, 200 refers to the screen position in pixels as x and y

coordinates.

myShtCtBar.ShowPopup 200,200

End Sub

Note To enable the disabled control, set the Enabled property to True.

Delete a submenu control

The following example code deletes the NewSub submenu that you created on

the Tools menu in the "Add a submenu" section:

Shortcut menu bars

A 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 bar

The following example code creates a new shortcut menu bar that is named

myShortcutBar:

Note The shortcut menu bar appears empty because no controls (menu items

or submenus) have been added to it.

Shortcut menus

Shortcut 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

Page 11 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 12: Excel Menubar Contrl VBA MicroSoft

Sub Shortcut_AddItem()

Dim myBar as Object

Set myBar = CommandBars("myShortcutBar")

With myBar

.Controls.Add (Type:=msoControlButton, before:=1).Caption

= "Item1"

.Controls("Item1").OnAction = "Code_Item1"

End With

myBar.ShowPopup 200,200

End Sub

Sub Shortcut_DisableItem()

Set myBar = CommandBars("myShortcutBar")

myBar.Controls("Item1").Enabled = False

myBar.ShowPopup 200,200

End Sub

Sub Shortcut_DeleteItem()

Set myBar = CommandBars("myShortcutBar")

myBar.Controls("Item1").Delete

myBar.ShowPopup 200,200

End Sub

Sub Shortcut_DeleteShortCutBar()

CommandBars("MyShortCutBar").Delete

End Sub

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 bar

The 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 bar

The following example code disables the Item1 command that you created in

the "Create a command on a shortcut menu" section:

Note To enable the disabled item, set the Enabled property to True.

Delete a command on a shortcut menu bar

The following example code deletes the menu command that is named Item1

on the myShortcutBar shortcut menu bar:

Delete a shortcut menu bar

Deleting 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:

Page 12 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 13: Excel Menubar Contrl VBA MicroSoft

Sub Shortcut_RestoreItem()

CommandBars("Cell").Reset

End Sub

Sub ShortcutSub_Create()

CommandBars("Cell").Controls.Add(Type:=msoControlPopup, before:=1)

_

.Caption = "NewSub"

' This displays the shortcut menu bar.

' 200, 200 refers to the screen position in pixels as x and y

coordinates.

CommandBars("Cell").ShowPopup 200, 200

End Sub

Sub ShortcutSub_AddItem()

Dim newSubItem as Object

Set newSubItem = CommandBars("Cell").Controls("NewSub”)

With newSubItem

.Controls.Add(Type:=msoControlButton, before:=1).Caption

= "subItem1"

' This will run the subItem1_Code macro when subItem1 is

clicked.

.Controls("subItem1").OnAction = "Code_subItem1"

End With

' This displays the Cell shortcut menu bar.

' 200, 200 refers to the screen position in pixels as x and y

coordinates

CommandBars("Cell").ShowPopup 200, 200

End Sub

Restore a command on a built-in shortcut menu bar

The following example code restores the default commands on the worksheet

Cell shortcut menu bar:

Submenus on shortcut menus

You 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 bar

The following example adds a new submenu that is named NewSub on the

worksheet Cell shortcut menu:

Note The submenu is empty because no menu items have been added to it.

Create a command control on a submenu that is located on a

shortcut menu bar

The 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:

Page 13 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 14: Excel Menubar Contrl VBA MicroSoft

Sub ShortcutSub_DisableItem()

CommandBars("Cell").Controls("NewSub") _

.Controls("subItem1").Enabled = False

' This displays the Cell shortcut menu bar.

' 200, 200 refers to the screen position in pixels as x and y

coordinates.

CommandBars("Cell").ShowPopup 200, 200

End Sub

Sub ShortcutSub_DeleteItem()

CommandBars("Cell").Controls("NewSub").Controls("subItem1").Delete

' This displays the Cell shortcut menu bar.

' 200, 200 refers to the screen position in pixels as x and y

coordinates.

CommandBars("Cell").ShowPopup 200, 200

End Sub

Sub ShortcutSub_DisableSub()

CommandBars("Cell").Controls("NewSub").Enabled = False

' This displays the Cell shortcut menu bar.

' 200, 200 refers to the screen position in pixels as x and y

coordinates.

CommandBars("Cell").ShowPopup 200, 200

End Sub

Sub ShortcutSub_DeleteSub()

CommandBars("Cell").Controls("NewSub").Delete

' This displays the Cell shortcut menu bar.

' 200, 200 refers to the screen position in pixels as x and y

coordinates.

CommandBars("Cell").ShowPopup 200, 200

End Sub

Disable a submenu item control on a shortcut menu

The following example code disables the SubItem1 command on the NewSub

submenu:

Note To enable a disabled item, set the Enabled property to True.

Delete a submenu item control on a shortcut menu

The following example deletes the SubItem1 command on the NewSub

submenu:

Disable a submenu control on a shortcut menu

The following example code disables the NewSub submenu on the Cell

shortcut menu bar:

Note To enable a disabled item, set the Enabled property to True.

Delete a submenu control on a shortcut menu

The following example code deletes the NewSub submenu that you created on

the Cell shortcut menu bar:

Page 14 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502

Page 15: Excel Menubar Contrl VBA MicroSoft

Microsoft Support ©2010 Microsoft

Find more information

The following resources are available to provide more information about how to

customize menus and menu bars in Excel.

Object Browser

The 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 Base

The 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/)

APPLIES TO

� Microsoft Office Excel 2003

� Microsoft Excel 2002 Standard Edition

� Microsoft Excel 2000 Standard Edition

Keywords: kbhowtomaster KB830502

Get Help Now

Contact a support professional by E-mail, Online, or Phone

Page 15 of 15How to customize menus and menu bars in Excel

25/05/2010http://support.microsoft.com/kb/830502