How to add a custom-made item to the shortcut menu [VBA]
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 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-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
- Press Alt-F11 to open visual basic editor
- Press with left mouse button on Module on the Insert menu
- Copy and paste vba code.
- Exit visual basic editor
Recommended articles
Save links to your favorite macros in a personal tab on the ribbon for easy access and become more productive. […]
Recommended articles
Macros and custom functions are great, they can automate many tedious tasks. To have them available whenever you need them, […]
Macro category
This article demonstrates how to add or remove a value in a regular drop down list based on a list […]
In this tutorial, I am going to show you how to add values to a drop down list programmatically in […]
This article demonstrates how to place values automatically to a table based on two conditions using a short macro. Cell […]
Excel categories
One Response to “How to add a custom-made item to the shortcut menu [VBA]”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Dear Oscar,
How are you?
I'm using this code and it works great in Excel (Microsoft 365). However, I found a bug or conflict and I can't solve it. The created menu item (right-click) is no longer displayed when used over "Format as Table".
Would you help me?
(https://www.get-digital-help.com/excel-2007-vba-create-a-custom-made-item-on-the-shortcut-menu/)
Thank´s
Best Regards