0 votes
in VBA by Beginner (3 points)
I have created a macro which can be started via clicking on a command button. It requests a password to get into an "admin" worksheet. This part works fine.

Now, I linked a "secret" shortcut to this macro. My goal is, to come into the admin sheet via this shortcut, without typing in a password.

The question, how can I differentiate, if the macro was started via clicking on the button or via the shortcut...

I can look which command button has started the macro with Application.Caller, but is it possible to register a shortcut-start?

Thanks for any comments.

1 Answer

0 votes
by Skilled (477 points)

Hi future profi,

You hit the nail on the head! Application.Caller [link] can do what you need. While it can't detect what key combination was clicked, you don't need that information. It will return "Error" if the macro wasn't called by a shape object, range or formula.

If you check out the link from Microsoft, you'll even see a code snippet that you can drop into your password-protected macro.

Here is a bit of code you can study. It includes the snippet:

Option Explicit

Public Sub ButtonClick()

    Dim v As String
    Select Case TypeName(Application.Caller)
        Case "Range"
            v = Application.Caller.Address
            Call AskForPassword
        Case "String"
            v = Application.Caller
            Call AskForPassword
        Case "Error"
            v = "Error"
            Call ProtectedMacro
        Case Else
            v = "unknown"
            Call AskForPassword
    End Select

MsgBox "caller = " & v

End Sub

Private Sub AskForPassword()

    ' Your code for getting password
    Dim Guess As String
    Guess = InputBox("Enter Password", "Halt! Who Goes There?")
    If Guess = "Open Sesame" Then
        Call ProtectedMacro
        MsgBox "Accessed Denied! Be Gone!", vbCritical, "Unauthorized Attempt"
    End If
End Sub

Private Sub ProtectedMacro()

    MsgBox "You're In", vbInformation + vbOKOnly, "Secret Passage"
End Sub





by Beginner (3 points)
Thanks, Mitch!

BR, Gyuri
by Skilled (477 points)
Glad to help. Plus, I learned something from you, too. :)

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.


For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.