Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA "OnKey" command problem

Status
Not open for further replies.

TomBarsh

Structural
Jun 20, 2002
1,003
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.

Thanks.
 
Replies continue below

Recommended for you

Hi TomBarsh:

The following code as you posted ...
Code:
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 ...
Code:
      Application.OnKey "{F1}"





Yogi Anand, D.Eng, P.E.
Energy Efficient Building Network LLC
ANAND Enterprises LLC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor