Multiple before right click options

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello all,

I have a custom right click for a series of ranges on on of my sheets. But I want to have a different range of cells with different right click options. is that possible?

here's a shortened version of my code:
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

Dim ContextMenu As CommandBar
Dim ctrl As CommandBarControl


Application.CommandBars("Cell").Reset


If Intersect(Target, Range("E89:R106,E108:R125,E127:R144,E146:R163")) Is Nothing Then
    Application.CommandBars("Cell").Reset
    Exit Sub
Else


    Set ContextMenu = Application.CommandBars("Cell")


    For Each ctrl In ContextMenu.Controls
            ctrl.Delete
    Next ctrl
    
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=1)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "RTO"
        .FaceId = 2113
        .Caption = Sheets("Settings").Range("K16") & " " & Sheets("Settings").Range("L16")
    End With
    
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=2)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "RTO_OTHER"
        .FaceId = 1845
        .Caption = Sheets("Settings").Range("K17") & " " & Sheets("Settings").Range("L17")
    End With
    
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=3)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "RTO_NOTE"
        .FaceId = 916
        .Caption = "CUSTOM NOTES"
        '.Font.Color = vbWhite
        '.Interior.Color = vbBlack
    End With
    


End If


End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi, have you considered something a structure similar to this:

Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)


If Not Intersect(Target, Range("E89:R106")) Is Nothing Then
    'Do stuff for range E89:R106
    Exit Sub
End If


If Not Intersect(Target, Range("E108:R125")) Is Nothing Then
    'Do stuff for range E108:R125
    Exit Sub
End If


'etc
'etc


Application.CommandBars("Cell").Reset


End Sub
 
Upvote 0
Here's my revised code: It's only using the first part though

Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

Dim ContextMenu As CommandBar
Dim ctrl As CommandBarControl


Application.CommandBars("Cell").Reset


If Intersect(Target, Range("E89:R89")) Is Nothing Then
    'Application.CommandBars("Cell").Reset
    Exit Sub
Else


    Set ContextMenu = Application.CommandBars("Cell")


    For Each ctrl In ContextMenu.Controls
            ctrl.Delete
    Next ctrl
    
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=1)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "RTO"
        .FaceId = 2113
        .Caption = Sheets("Settings").Range("K16") & " " & Sheets("Settings").Range("L16")
    End With
    
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=2)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "RTO_OTHER"
        .FaceId = 1845
        .Caption = Sheets("Settings").Range("K17") & " " & Sheets("Settings").Range("L17")
    End With
    
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=3)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "RTO_NOTE"
        .FaceId = 916
        .Caption = "CUSTOM NOTES"
        '.Font.Color = vbWhite
        '.Interior.Color = vbBlack
    End With
    


End If


If Intersect(Target, Range("D89:D92")) Is Nothing Then
    'Application.CommandBars("Cell").Reset
    Exit Sub
Else


    Set ContextMenu = Application.CommandBars("Cell")


    For Each ctrl In ContextMenu.Controls
            ctrl.Delete
    Next ctrl
    


    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=1)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "RTO_CUSTOM"
        .FaceId = 484
        .Caption = Sheets("Settings").Range("K21") & " " & Sheets("Settings").Range("L21")
    End With
    
    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=2)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "CLEAR"
        .FaceId = 2087
        .Caption = "*!CLEAR REQUESTS!*"
    End With
End If
Application.CommandBars("Cell").Reset
End Sub
 
Upvote 0
I see it... "Not" That completely reversed the entire thing and made it work....:eeek::laugh:

Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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