Macro Sheets Only
Adds a command to a menu. ADD.COMMAND returns the position number on the menu of the added command. Use ADD.COMMAND to add one or more custom menu commands to a menu on a built-in or custom menu bar. You can also use ADD.COMMAND to restore a deleted built-in command to its original menu.
Syntax
ADD.COMMAND(bar_num, menu, command_ref, position1, position2)
Bar_num is the number corresponding to a menu bar or a type of shortcut menu to which you want to add a command.
The ID numbers of the built-in menu bars and the types of shortcut menus are listed in the following tables. Short menus are abbreviated versions of the normal Microsoft Excel menus. To turn on short menus, use the SHORT.MENUS function.
Bar_num Built-in menu bar
1 Worksheet and macro sheet (Microsoft Excel 4.0 or later)
2 Chart (Microsoft Excel 4.0 or later)
3 Null (the menu displayed when no workbooks are open)
4 Info
5 Worksheet and macro sheet (short menus, Microsoft Excel 3.0 and earlier)
6 Chart (short menus, Microsoft Excel 3.0 and earlier)
7 Cell, toolbar, and workbook (shortcut menus)
8 Object (shortcut menus)
9 Chart (Microsoft Excel 4.0 or later shortcut menus)
10 Worksheet and Macro sheet
11 Chart
12 Visual Basic
Menu is the menu to which you want the new command added.
Bar_num |
Menu |
Shortcut menu |
7 |
1 |
Toolbars |
7 |
2 |
Toolbar buttons |
7 |
3 |
Workbook paging icons in Excel 4.0 |
7 |
4 |
Cells (worksheet) |
7 |
5 |
Column selections |
7 |
6 |
Row selections |
7 |
7 |
Workbook tabs |
7 |
8 |
Cells (macro sheet) |
7 |
9 |
Workbook titlebar |
7 |
10 |
Desktop (Microsoft Excel for Windows only) |
7 |
11 |
Module |
7 |
12 |
Watch pane |
7 |
13 |
Immediate pane |
7 |
14 |
Debug code pane |
8 |
1 |
Drawn or imported objects on worksheets, dialog sheets, and charts. |
8 |
2 |
Buttons on sheets |
8 |
3 |
Text boxes |
8 |
4 |
Dialog sheet |
9 |
1 |
Chart series |
9 |
2 |
Chart and Axis titles |
9 |
3 |
Chart plot area and walls |
9 |
4 |
Entire chart |
9 |
5 |
Chart axes |
9 |
6 |
Chart gridlines |
9 |
7 |
Chart floor and arrows |
9 |
8 |
Chart legend |
Note Any commands that you add to the toolbar buttons, watch pane, immediate pane or debug code pane shortcut menus will be dimmed.
Command_ref is an array or a reference to an area on the macro sheet that describes the new command or commands.
Position1 specifies the placement of the new command.
Position2 specifies the placement of the new command on a submenu.
Tip In general, use menu and command names rather than numbers for arguments. The numbers assigned to menus and commands change as you add and delete menus and commands. Using names ensures that your menu and command macro functions always refer to the correct items.
Example
The following macro formula adds the command described in cells G16:J16 to the bottom of the worksheet cells shortcut menu:
ADD.COMMAND(7, 4, G16:J16)
Related Functions
ADD.BAR Adds a menu bar
ADD.MENU Adds a menu to a menu bar
ADD.TOOL Adds one or more buttons to a toolbar
ADD.TOOLBAR Creates a toolbar with the specified tools
DELETE.COMMAND Deletes a command from a menu
ENABLE.COMMAND Enables or disables a menu or custom command
GET.TOOLBAR Retrieves information about a toolbar
RENAME.COMMAND Changes the name of a command or menu
List of Customizing Functions