Right-Click Option Caption

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,600
Office Version
  1. 365
Platform
  1. Windows
I am using some code to add some options to the cell right click function
1712919298698.png


When a cell is right clicked, there will be a number of options shown, in the above these are EAST and MIDS.

When one of these options is clicked, I need to be able to return which one is clicked as there is a macro that will run when an option is selected and I need the text string of the option selected to be in memory so I can drop it into a list.


TIA
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
See if this will work for you.

First, let's assume that EAST calls your macro called "MyMacro1", and MIDS calls your macro called "MyMacro2".

Now, in that same module, declare a module-level string variable to hold the name of the option selected, let's call it menuItemCaller. (Note: If this value is needed in a separate module, declare it as Public.)

Then, for both "MyMacro1" and "MyMacro2", add a line of code that assigns the name of the option selected to your variable menuItemCaller.

Then, when you want to drop it into a list, simply refer to the variable menuItemCaller.

Here's an example...

VBA Code:
Option Explicit

Dim menuItemCaller As String

Sub MyMacro1()

    menuItemCaller = "EAST"
 
    'etc
    '
    '

End Sub

Sub MyMacro2()

    menuItemCaller = "MIDS"
 
    'etc
    '
    '
 
End Sub

Sub DropIntoList()

    MsgBox menuItemCaller & " was the last menu item clicked!", vbInformation
 
    'etc
    '
    '
    '
 
    'menuItemCaller = ""
 
End Sub

Hope this helps!
 
Upvote 0
The items in the right click are dynamic and are defined from a list that is looped through.

So dependent on the cell that is right clicked, this list will be different.

Because of this, I am using one macro to be called upon any of the items being clicked but I need to know which item was clicked prior to the macro executing.
 
Upvote 0
In that case, maybe you can use a class module, which would allow you to handle multiple controls with one event handler. Here's an example that hopefully you can adopt and amend as needed. I've assumed that the items are listed in Column A, starting at cell A2, and are located on "Sheet1" within the workbook running the code. Note that in this example, addShortCutMenuItems() is used to add your items to the shortcut menu, and deleteShortCutMenuItems() is used to remove them from the same. And you would likely want to use the workbook open event to call addShortCUtMenuItems() when the workbook opens, and then use the workbook close event to call deleteShortCutMenuItems() when the workbook is closed.

1) Insert a class module (Visual Basic Editor >> Insert >> Class Module), and call it clsMenuItem...

VBA Code:
Option Explicit

Public WithEvents menuItem As Office.CommandBarButton

Private Sub menuItem_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    Debug.Print Ctrl.Caption 'you can add it to a collection or dictionary so that you could have the list available when needed
End Sub

2) In a regular module (Visual Basic Editor >> Insert >> Module)...

VBA Code:
Option Explicit

Dim colShortCutMenuItems As Collection

Public Sub addShortCutMenuItems()

    Dim objMenuItem As clsMenuItem
    Dim objMenuBar As CommandBar
    Dim objControl As CommandBarButton
    Dim rngMenuList As Range
    Dim rngCell As Range
   
    Set colShortCutMenuItems = New Collection

    Set objMenuBar = Application.CommandBars("cell")
   
    With ThisWorkbook.Worksheets("Sheet1")
        Set rngMenuList = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
   
    For Each rngCell In rngMenuList
   
        Set objControl = objMenuBar.Controls.Add(Type:=msoControlButton, temporary:=True)
        With objControl
            .Caption = rngCell.Value
            .OnAction = "YourMacroName" 'change as required
        End With
       
        Set objMenuItem = New clsMenuItem
        Set objMenuItem.menuItem = objControl
       
        colShortCutMenuItems.Add objMenuItem
       
    Next rngCell
   
End Sub

Public Sub deleteShortCutMenuItems()

    Dim obj As Object
   
    On Error Resume Next
    For Each obj In colShortCutMenuItems
        Application.CommandBars("cell").Controls(obj.menuItem.Caption).Delete
    Next obj
    On Error GoTo 0
   
End Sub

Hope this helps!
 
Last edited:
Upvote 0
you can pass the control caption to a general callback macro - then based on the caption the macro will choose what code will run next.
so you have a basic OnAction macro:
VBA Code:
sub onActionMacro (ctlCaption as string)
select case ctlCaption 
Case "EAST" 
.....
case "MIDS"
...
Case else
....
end select
end sub
When you add controls to the context menu you do it like this:
VBA Code:
        With .Controls.Add(1)  'msoControlButton = 1
            .Caption = "EAST"
            .OnAction = "'onActionMacro """ & .Caption & """'"
            .Style = msoButtonIconAndCaption
            .FaceId = 9139
            .BeginGroup = False
            .Tag = ""
        End With
The apostrophes are essential.
Or you can use the Tag property and go wild with the captions :)
 
Upvote 0

Forum statistics

Threads
1,215,217
Messages
6,123,673
Members
449,116
Latest member
HypnoFant

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