Excel VBA "OnKey" command problem
Excel VBA "OnKey" command problem
(OP)
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.
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.





RE: Excel VBA "OnKey" command problem
The following code as you posted ...
CODE
Application.OnKey "{F1}", "tom2"
End Sub
'
Sub tom2()
'some action to be taken
End Sub
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
Yogi Anand, D.Eng, P.E.
Energy Efficient Building Network LLC
ANAND Enterprises LLC
http://www.energyefficientbuild.com