It is possible with a few lines of code in the ThisWorkbook
module of your Personal file:
Option Explicit
Public WithEvents App As Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Private Sub App_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Debug.Print "RightClick on Sheet: " & Sh.Name & " at " & Target.Address
End Sub
(note that after adding the code, you have to save, close Excel completely and restart it so that the Workbook_Open
event is fired).
However, there are two things to consider:
- When you set
Cancel = True
, you steal all the context menus - I would not recommend to do so. However, if you set Cancel = False
, there will be two actions in parallel, first your action, and only after that action is finished (which may be a long time when you show a form), the context menu will pop up - very confusing.
- There are several cases where global Vars are reset, see discussion here: What is the lifetime of a global variable in excel vba? Once one of these events happens, your
app
variable is reset and from that point on the events will no longer fire until you restart Excel.
My suggestion: Assign a Keyboard Shortcut to a Macro in your personal workbook. Instead of Right Click, the event to fire would be for example Ctrl+Shift+F
to Format your worksheet.