Author: Oscar Cronquist Article last updated on January 29, 2019

This post describes how to add a new custom-built item to the shortcut menu in Excel, when you press with right mouse button on a cell a context menu appears. Let's add a new item to the cell context shortcut menu and link it to a macro.

I demonstrated in a previous post how to automatically select cell A1 on every sheet in a workbook using vba. This is the macro we are going to use. This macro selects cell A1 in all sheets in the active workbook. Copy the following code to a regular code module, see details below.

Macro VBA code:

Sub SelectA1()
    Dim sht As Worksheet, csheet As Worksheet

    Application.ScreenUpdating = False
    Set csheet = ActiveSheet

    For Each sht In ActiveWorkbook.Worksheets
      If sht.Visible Then
        ActiveWindow.ScrollRow = 1
        ActiveWindow.ScrollColumn = 1
      End If
    Next sht

    Application.ScreenUpdating = True
End Sub

This vba code adds an item on the cell shortcut menu. Copy the code into the standard code module.

Sub AddItemShortCutMenu()
Dim Shortcut As CommandBar
Dim NewItem As CommandBarButton
Set Shortcut = Application.CommandBars("Cell")
Set NewItem = Shortcut.Controls.Add(Type:=msoControlButton)
With NewItem
.OnAction = "SelectA1"
.Caption = "Select cell A1 in all sheets in this workbook"
End With
End Sub

This vba code removes the item on the cell shortcut menu. Copy the code into the standard code module.

Sub RemoveItemFromShortCutMenu()
On Error Resume Next
CommandBars("Cell").Controls("Select cell A1 in all sheets in this workbook").Delete
End Sub

The code below adds the item on the shortcut menu whenever this workbook is opened.  It also deletes the Item from the shortcut menu whenever the workbook is closed. Copy the code below and paste it into the ThisWorkbook code window. You open ThisWorkbook code window by double-press with left mouse button oning ThisWorkbook in the project explorer.

Private Sub Workbook_Open()
    Call AddItemShortCutMenu
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call RemoveItemFromShortCutMenu
End Sub

Create an add-in

Now if you save the workbook as an add-in and install the add-in, the added item on the shortcut menu is always available.

How to create an add-in: Save your custom functions and macros in an Add-In

How to copy vba code into a standard code module

  1. Press Alt-F11 to open visual basic editor
  2. Press with left mouse button on Module on the Insert menu
  3. Copy and paste vba code.
  4. Exit visual basic editor

Get the Excel file


Recommended articles

Customize the ribbon and how to add your macros
Save links to your favorite macros in a personal tab on the ribbon for easy access and become more productive. […]

Recommended articles

How to save custom functions and macros to an Add-In
Macros and custom functions are great, they can automate many tedious tasks. To have them available whenever you need them, […]