Some subs I only ever want to run from the immediate window, like this slightly risky one:
Public Sub clear()
Application.SendKeys "^a", True
Application.SendKeys "{delete}", True
End Sub
In case you can't work it out; it sends ctrl+a and Del to the application, and has the effect of clearing whatever is around the cursor at the time.
I use it in the immediate window by typing clear
and everything in the immediate window is removed.
As you can probably tell, this Sub is very dangerous and relatively simple to accidentally call. Therefore I would like to have it run only when called from the immediate window
- Not from another Sub
- Not from a Form Control
- Not from ActiveX (although that's effectively just a sub)
- Not from a user call (through the Developer/Macros tab)
And not from any other source either (I've listed all those, because I think even if you can't directly tell that it was called by the immediate window, you might be able to tell indirectly by ruling out the other options)
Is this possible to do programatically? Ideally, I just want to have a simple Boolean check at the start of my clear()
sub, telling it to do nothing unless called by the immediate window.
N.B.
One route might be through the use of the Call Stack (ctrl+L to open the dialogue box), as I notice immediate calls leave no trace in the stack whereas subs calling clear()
are listed and so can be ruled out. But I'm not sure how to access this through VBA