Option Explicit
Private Sub Workbook_Deactivate()
On Error Resume Next
With Application
.CommandBars("Cell").Controls("RunIt").Delete
End With
On Error GoTo 0
End Sub
'Add Excel Right Click Menu
'Excel VBA: Adding a Command Button to the Excel Right Click Pop-up Toolbar/Command Bar
'Current Special! Complete Excel Excel Training Course for Excel 97 - Excel 2003, only $145.00. $59.95 Instant Buy/Download, 30 Day Money Back Guarantee & Free Excel Help for LIFE!
'Adding a Command Button to the Excel Right Click Pop-up Toolbar/Command Bar
'Note how the code first deletes any existing menu option called "My Macro". This stops the code adding an additional custom menu options and ensures only one custom menu option is available at any one time. The use of Temporary:=True ensures this menu option is deleted whenever Excel closes. Without it, it's possible to have you custom menu option showing in another Workbook. This is also why we make use of the Workbook_Deactivate Event. That is, if we activate another Workbook while this one is open, the custom menu option is deleted.
'The macro that is run whenever this custom menu option is clicked is called "My_Macro". You should change this, and the caption, to suit your needs.
'The code below should be placed in Private Module of the Workbook Object (ThisWorkbook). To get there quickly, right click on the Excel icon, top left next to File and choose View Code.
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Dim cBut As CommandBarButton
On Error Resume Next
With Application
.CommandBars("Cell").Controls("RunIt").Delete
Set cBut = .CommandBars("Cell").Controls.Add(Temporary:=True)
End With
With cBut
.Caption = "RunIt"
.Style = msoButtonCaption
.OnAction = "RunIt"
End With
On Error GoTo 0
End Sub