adding sub menu on right click

JeanKim

Board Regular
Joined
May 31, 2016
Messages
183
Code:
Sub Add_Menu ()
     With Application.CommandBars("cell")
          .Reset
          With .Controls.Add (Tpe:=msControlButton, temporary:=False)
               .Caption = "Menu 1"
               .OnAction = "Men1"
          End With
     End with
End Sub
Above is code I used to add menu on right click.
Is it possible to add sub menu using VBA?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
This will work on a right click... added a faceid for you as well! I have added this to the top of the menu using the before:=1 option to show you that it can be moved in the menu

Code:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

[COLOR="#008080"]  ' Dim ContextMenu As CommandBar[/COLOR] <-not needed but left in the code in case any errors occur and I can turn it back on

   Set ContextMenu = Application.CommandBars("Cell")
   
   
  With Application.CommandBars("cell")
          .Reset
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=1)
                .FaceId = 59
               .Caption = "Menu 1"
               .OnAction = "Men1"
          End With
     End With
End Sub

You could of course move this code to run when the workbook is opened each time...just a thought!
 
Last edited:
Upvote 0
This will work on a right click... added a faceid for you as well! I have added this to the top of the menu using the before:=1 option to show you that it can be moved in the menu

Code:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

[COLOR=#008080]  ' Dim ContextMenu As CommandBar[/COLOR] <-not needed but left in the code in case any errors occur and I can turn it back on

   Set ContextMenu = Application.CommandBars("Cell")
   
   
  With Application.CommandBars("cell")
          .Reset
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=1)
                .FaceId = 59
               .Caption = "Menu 1"
               .OnAction = "Men1"
          End With
     End With
End Sub

You could of course move this code to run when the workbook is opened each time...just a thought!

Thank you for quick reply.

Now, is it possible to create SUB_Menu? (create black triangle to open sub menu)
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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