Adding Items to a Context Menu

by Allen Wyatt
(last updated October 8, 2019)


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.


If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2064) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Saving Your Work Automatically

Word can be configured to save your work periodically, on any time schedule you desire. This tip explains this feature ...

Discover More

Adding an Ellipsis to the Beginning of Some Paragraphs

The Find and Replace feature of Word is very powerful. You can even use it to add a unique character to the beginning of ...

Discover More

Dealing with Text Length Limits and AutoFilter Drop-Down Lists

Excel has some built-in limits on what you can do with the program. When you run into those limits, it can be frustrating ...

Discover More

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!

More ExcelTips (menu)

Removing Items from a Context Menu

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 More

Resetting Dynamic Menus

Excel's menus, by default, only display the most common commands that you use. After a time you may want to reset the ...

Discover More

Animated Menus

The menus used in Excel can be animated in several different ways. All it takes is a quick change to the Excel options.

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 3 - 3?

2021-01-22 08:08:21


I notice this context menu item persists across all sheets.

I moved to Workbook Workbook_SheetBeforeRightClick event

Now, if I wanted to add a parameter to the .onAction sub, how would it be?

2020-12-23 22:21:41


I've tried what you suggest in your first code block:

Sub AddItem()
Dim cmdNew As CommandBarButton
Set cmdNew = CommandBars("cell").Controls.Add

'With cmdNew
'.Caption = "Insert check hyperlink"
'.OnAction = "InsertCheckHyperlink()"
'.BeginGroup = True
'End With
End Sub

When I step through the routine to the "Set" command, it stops with the error "Object variable or With block variable not set." (I've commented out the rest of the routine just to be sure it isn't somehow causing the error.) Since the same code seems to work for you, why am I getting an error? Thanks.

2020-02-04 00:37:24


is there a way to insert sub-menus?

2017-03-16 10:42:46



I am creating a large report which is connected to SQL so I will have a menu sheet from cell A1 downwards I will list let’s say top 10 sales people, top then products etc. in cell B1 I will have SQL query
the question is ? Is it possible that .Caption = sheets("menu"). range ("A1:A20")
I am new to it I would be grateful if you could put some notes how you approached
Thank you,

2016-08-12 11:23:59


Is it possible to replace a command like "Copy" so that it is in the same location?

2015-11-06 13:02:54


you solved a difficult proble to get get through

2015-11-06 12:12:10


very nice

2015-05-10 09:11:58

Kadr Leyn

Thanks for information.
I made template this topic and I added a date userform to right-click(context) menu.In this way you can add "date" with right-click menu to selected cell.
Link :

2015-03-05 04:01:35

Jeff Weir

Whoops, I meant
if .Caption = "My Procedure" then icbc.Delete

2015-03-05 04:00:30

Jeff Weir

Hi Allan. Great article.

Question: Is there any particular reason you use the .Tag property in the following line:
If icbc.Tag = "brccm" Then icbc.Delete
...instead of just using the .Caption property like so:
.Caption = "My Procedure"


This Site

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.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.