This post describes how to add a new custom-built item to the shortcut menu in excel 2007.

In excel 2007 when you right click a cell, a cell 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 code into a standard code module.

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
        sht.Activate
        Range("A1").Select
        ActiveWindow.ScrollRow = 1
        ActiveWindow.ScrollColumn = 1
      End If
    Next sht

    csheet.Activate
    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 clicking 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. Click Module on the Insert menu
  3. Copy and paste vba code.
  4. Exit visual basic editor

Download excel file

Select A1.xlsm
Excel 2007 *.xlsm

Recommended articles