Written by Allen Wyatt (last updated November 12, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003
When you right-click on a cell, Excel provides you with a feature-rich Context menu that allows you to do any number of things. You may want to add some features to that Context menu, particularly if they are features you use often.
Unfortunately, you cannot edit Context menus in the same manner that you can edit other menus—by using Customize from the Tools menu. Instead, you must manipulate Context menus through VBA.
If you want to add an item to the Context menu that appears when you right-click on a cell, you can use the following code:
Sub AddItemToContextMenu() Dim cmdNew As CommandBarButton Set cmdNew = CommandBars("cell").Controls.Add With cmdNew .Caption = "My Procedure" .OnAction = "MyProcedure" .BeginGroup = True End With End Sub
All you need to do is set the .Caption property to whatever menu text you want used, and then change the .OnAction property so that it points to a macro or command you want used. When you later want to remove the menu option, you can use the following macro:
Sub RemoveContextMenuItem() On Error Resume Next CommandBars("cell").Controls("My Procedure").Delete End Sub
To use this, change the "My Procedure" text to whatever text you used in the .Caption property of the previous macro. The On Error statement is used in this macro just in case the specified macro item had not been previously added.
By modifying your macro just a bit, you can specify that the addition to the Context menu should occur only when right-clicking on specific cells. The following macro checks to see if you are clicking on a cell in the range of C10:E25. If you are, then it adds a menu option for your procedure at the end of the Context menu.
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, _ Cancel As Boolean) Dim cmdNew As CommandBarButton For Each icbc In Application.CommandBars("cell").Controls If icbc.Tag = "brccm" Then icbc.Delete Next icbc If Not Application.Intersect(Target, Range("c10:e25")) _ Is Nothing Then Set cmdNew = CommandBars("cell").Controls.Add With cmdNew .Caption = "My Procedure" .OnAction = "MyProcedure" .BeginGroup = True .Tag = "brccm" End With End If End Sub
In the VBA editor, this macro needs to be added to the specific worksheet that you want it used with. All you need to do is double-click on that worksheet, in the Project Explorer (upper-left corner of the VBA editor), and then enter it into the code window for that worksheet.
As with the earlier macro, all you need to do is modify the settings specified for the .Caption and .OnAction properties. In addition, you may want to change the cell range that is considered "valid" when adding a menu choice—just change the "c:10:e25" range specification to the range desired. You can even use a named range in place of the cell range, which is great if your valid range is really a set of non-contiguous cells.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2064) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Context menus appear when you right-click an item in Excel. If you want to modify the menu that appears, the way to do so ...
Discover MoreThe menus used in Excel can be animated in several different ways. All it takes is a quick change to the Excel options.
Discover MoreExcel allows you to customize your menus so that they contain the commands you want on them. If you later want to delete ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments