Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Excel VBA "OnKey" command problem

Excel VBA "OnKey" command problem

Excel VBA "OnKey" command problem

I want to use the 'OnKey' VBA command so that some action is taken when a particular key is pressed.

I tried the Excel VBA Help examples, Walkenbach's examples from Power Programming, and a few examples from internet resources. All seemed to use the same basic syntax but it doesn't work for me. Yes, I can use the OnKey command to turn off or restore action. The problem is with directing the action to be taken to a new routine.

For example,

   Sub tom1()
      Application.OnKey "{F1}", "tom2"
   End Sub
   Sub tom2()
      'some action to be taken
   End Sub

Gives error message:  "The macro "c:\myfile.xls'!tom1' cannot be found."

What I finally found is that the macro (in this case, "tom2") was found if all of the code was in the Personal.xls file. Can this be gotten to work if it's in a different file? I have several other files that serve as my "personal.xls" file and I don't really want to be forced to use this file as well.


RE: Excel VBA "OnKey" command problem

Hi TomBarsh:

The following code as you posted ...


Sub tom1()
      Application.OnKey "{F1}", "tom2"
End Sub
Sub tom2()
      'some action to be taken
End Sub
works for me. Please note you have to run the macro tom1 first for the F1 key to call and execute the macro tom2.

After I ran the macro tom1, pressing the key F1 called macro tom2 to be run.

And then of course, after testing it I reset the F1 key to its default status by using the statement ...


      Application.OnKey "{F1}"

Yogi Anand, D.Eng, P.E.
Energy Efficient Building Network LLC
ANAND Enterprises LLC

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close