VBA – Modify the Right-Click Menu to Call a Macro
Right-Click to Call a Macro
Here is some code that will allow a user to select your macro from the default menu that appears after they right click a cell.
1. Put the following code in the ThisWorkbook code window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Private Sub Workbook_Open() Dim MyMenu As Object Set MyMenu = Application.ShortcutMenus(xlWorksheetCell) _ .MenuItems.AddMenu("This is my Custom Menu", 1) With MyMenu.MenuItems .Add "MyMacro1", "MyMacro1", , 1, , "" .Add "MyMacro2", "MyMacro2", , 2, , "" End With Set MyMenu = Nothing End Sub |
2. Put the following code in a module
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Public Sub mymacro1() MsgBox "Macro1 from a right click menu" End Sub Public Sub mymacro2() MsgBox "Macro2 from a right click menu" End Sub |
3. Close your workbook and re-open!
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!