VBA for Right Click Macros

andrewb90

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

I have been searching the web for the code to do what I am looking for, but haven't had much luck, so I'm hoping someone on here will be able to help :)

I would like to have 2 different macros appear in the right click menu, however I would like to only have this appear on 1 sheet, and only on selected cells on the sheet.
Is this doable?

Thank you,

Andrew
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Andrew,

This has worked when testing;

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("A1:A5")) Is Nothing Then
    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 & "'!" & "MyMacro1"
        .FaceId = 59
        .Caption = "MyMacro Number 1"
    End With

    With ContextMenu.Controls.Add(Type:=msoControlButton, before:=2)
        .OnAction = "'" & ThisWorkbook.Name & "'!" & "MyMacro2"
        .FaceId = 59
        .Caption = "MyMacro Number 2"
    End With
End If

End Sub

Then to make sure the menu resets if you click straight to another sheet

Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)

Application.CommandBars("Cell").Reset

End Sub

Hope this helps,
Cheers,
Alan.
 
Upvote 0
Where do I place the second macro? Right below the first or does that need to be somewhere else?
 
Upvote 0
Awesome! thank you both!

1 thing I noticed is the specified range doesn't seem to be affecting which cells have the unique right click menu. Isn't
Code:
If Intersect(Target, Range("E89:R106")) Is Nothing Then    Exit Sub
Else

That supposed to determine the range?
 
Last edited:
Upvote 0
Happy to help :)

Is it only affecting other cells after the specified range has been right clicked? Probably need to add a reset before exiting the sub...

Code:
If Intersect(Target, Range("E89:R106")) Is Nothing Then
    [COLOR="#FF0000"]Application.CommandBars("Cell").Reset[/COLOR]
    Exit Sub
Else
 
Upvote 0
I did that, but I don't see any change, I also noticed that all my other sheets have that custom right click menu now as well.
 
Upvote 0
Ok, I feel a little dumb, I looked back at This Workbook sheet and realized I had deleted the other piece of code. Now with the little change everything appears to be working. :eeek:
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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