Run Macros From Context Menu

alexfooty

Board Regular
Joined
Dec 30, 2018
Messages
97
Office Version
  1. 2016
Platform
  1. Windows
Hi
If I have two macros, Macro1 & Macro2, is it possible to right click on any sheet in the workbook and run these macros from the context menu?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi T0ny84
Thanks for your quick reply
Getting syntax error ".MenuItems.AddMenu("This is my Custom Menu", 1)"
Is it something I need to modify?
 
Upvote 0
In my experience, it's better to use macro in customized ribbon rather than using in right button, because some times macro don't show up in contexmenu for all files, just sharing my experience.
 
Upvote 0
Hi T0ny84
Thanks for your quick reply
Getting syntax error ".MenuItems.AddMenu("This is my Custom Menu", 1)"
Is it something I need to modify?
I just tried this and it worked, try this and let me know if you have any issues.

Put the following into the ThisWorkbook:
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' Removes the added menu when the workbook is closed.
Application.CommandBars("Cell").Reset
End Sub

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 "NameWantInMenu", "MacroName", ,OrderNumber, , ""
 .Add "MyMacro1", "MyMacro1", , 1, , ""
 .Add "MyMacro2", "MyMacro2", , 2, , ""
End With
Set MyMenu = Nothing
End Sub

On a Module:
VBA Code:
Public Sub mymacro1()
MsgBox "Macro1 from a right click menu"
End Sub
Public Sub mymacro2()
MsgBox "Macro2 from a right click menu"
End Sub

Save the workbook as a Macro Enabled Workbook then close and reopen.
I changed: .Add "MyMacro1", "MyMacro1", , 1, , ""
To: .Add "Bom", "MyMacro1", , 1, , ""

1599604791153.png
 
Upvote 0
To reset a specific menu you can use below, please note that they will also remove any other customization you have to the menu

Right Click On A Cell
Application.CommandBars("Cell").Reset
Application.CommandBars("cell").Enabled = True

Right Click On A Column
Application.CommandBars("Column").Reset
Application.CommandBars("column").Enabled = True

Right Click On A Command Bar
Application.CommandBars("Row").Reset
Application.CommandBars("row").Enabled = True
 
Upvote 0
t0ny84 - Thank you so much for your help with this - it works perfectly. Thank you
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top