Adding sub-menu groups to contect menu

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
I have code that I've used for yonks to create a custom right-click context menu. It's getting long and I'm wondering how to create sub menus to group related macros. Here's my code:

VBA Code:
Private Sub Workbook_Open()

Dim contextMenu As CommandBar

Set contextMenu = Application.CommandBars("Cell")

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=30)
     .OnAction = "GeneralComment"
     .FaceId = 31
     .Caption = "Add Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=31)
     .OnAction = "DeleteGeneralComment"
     .FaceId = 31
     .Caption = "Delete Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=32)
     .OnAction = "OptionComment"
     .FaceId = 31
     .Caption = "Add Options Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=33)
     .OnAction = "DeleteOptionsComment"
     .FaceId = 31
     .Caption = "Delete Options Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=34)
     .OnAction = "AddComment"
     .FaceId = 31
     .Caption = "Sent to CC Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=35)
     .OnAction = "AddCommentReturn"
     .FaceId = 31
     .Caption = "Returned back from CC Comment"
     .Tag = "My_Cell_Control_Tag"
End With

With contextMenu.Controls.Add(Type:=msoControlButton, Before:=36)
     .OnAction = "DeleteComment"
     .FaceId = 31
     .Caption = "Delete Sent to CC Comment"
     .Tag = "My_Cell_Control_Tag"
End With

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Perhaps something like this ...

VBA Code:
Private Sub Workbook_Open()

    Dim contextMenu As CommandBar
    Dim myPopUp     As CommandBarPopup                      ' <<<<<<

    Set contextMenu = Application.CommandBars("Cell")

    With contextMenu.Controls
        With .Add(Type:=msoControlButton, Before:=30)
            .OnAction = "GeneralComment"
            .FaceId = 31
            .Caption = "Add Comment"
            .Tag = "My_Cell_Control_Tag"
        End With
    
        With .Add(Type:=msoControlButton, Before:=31)
            .OnAction = "DeleteGeneralComment"
            .FaceId = 31
            .Caption = "Delete Comment"
            .Tag = "My_Cell_Control_Tag"
        End With

        Set myPopUp = .Add(Type:=msoControlPopup)           ' <<<<<<

    End With


    With myPopUp.Controls                                   ' <<<<<<
        With .Add(Type:=msoControlButton)
            .OnAction = "OptionComment"
            .FaceId = 31
            .Caption = "Add Options Comment"
            .Tag = "My_Cell_Control_Tag"
        End With

        With .Add(Type:=msoControlButton)
            .OnAction = "DeleteOptionsComment"
            .FaceId = 31
            .Caption = "Delete Options Comment"
            .Tag = "My_Cell_Control_Tag"
        End With
    End With

End Sub
 
Upvote 0
Perhaps something like this ...

VBA Code:
Private Sub Workbook_Open()

    Dim contextMenu As CommandBar
    Dim myPopUp     As CommandBarPopup                      ' <<<<<<

    Set contextMenu = Application.CommandBars("Cell")

    With contextMenu.Controls
        With .Add(Type:=msoControlButton, Before:=30)
            .OnAction = "GeneralComment"
            .FaceId = 31
            .Caption = "Add Comment"
            .Tag = "My_Cell_Control_Tag"
        End With
   
        With .Add(Type:=msoControlButton, Before:=31)
            .OnAction = "DeleteGeneralComment"
            .FaceId = 31
            .Caption = "Delete Comment"
            .Tag = "My_Cell_Control_Tag"
        End With

        Set myPopUp = .Add(Type:=msoControlPopup)           ' <<<<<<

    End With


    With myPopUp.Controls                                   ' <<<<<<
        With .Add(Type:=msoControlButton)
            .OnAction = "OptionComment"
            .FaceId = 31
            .Caption = "Add Options Comment"
            .Tag = "My_Cell_Control_Tag"
        End With

        With .Add(Type:=msoControlButton)
            .OnAction = "DeleteOptionsComment"
            .FaceId = 31
            .Caption = "Delete Options Comment"
            .Tag = "My_Cell_Control_Tag"
        End With
    End With

End Sub

This works nicely. Is it possible to have a caption for the sub menu? With your code it's just blank with an arrow.
 
Upvote 0
I could have been a little more elaborate. So try ...

VBA Code:
    Dim contextMenu As CommandBar
    Dim myPopUp     As CommandBarPopup                      ' <<<<<<

    Set contextMenu = Application.CommandBars("Cell")

    With contextMenu.Controls
        Set myPopUp = .Add(Type:=msoControlPopup)           ' <<<<<<
        With myPopUp
            .Caption "All About Comments"
            .Tag = "My_Cell_Control_Tag"
        End With
    End With
 
Upvote 0
Solution
I could have been a little more elaborate. So try ...

VBA Code:
    Dim contextMenu As CommandBar
    Dim myPopUp     As CommandBarPopup                      ' <<<<<<

    Set contextMenu = Application.CommandBars("Cell")

    With contextMenu.Controls
        Set myPopUp = .Add(Type:=msoControlPopup)           ' <<<<<<
        With myPopUp
            .Caption "All About Comments"
            .Tag = "My_Cell_Control_Tag"
        End With
    End With

Perfect. Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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