• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Question on Application.OnKey "{ENTER}"

inddon

Member
Hello There,

I have mentioned the below code in workbook :

Code:
Private Sub Workbook_Open()
....
....

Application.OnKey "{ENTER}" "ExecuteCode"
....
End sub

In the module ExecuteCode code as below:

Code:
Sub ExecuteCode

<do some code>

End Sub


When I open another workbook, the code form the first file gets executed on Enter Key.

I would like the code Application.OnKey "{ENTER}" to get working only for the first workbook and not any of the open workbooks and they should work in a normal way when Enter key pressed.

Could you please advise how this can be achieved?

Thanks & regards,
Don
 
Why trigger code on Enter? Application.OnKey isn't all that reliable and best avoided.

Why not just Application.Run?
 
Hi Chihiro,

Thank you for your reply.

In the worksheet, I have a few cells defined as range names. When the User presses 'Enter' key on those cell (which has a range name), then it should execute the code.

I came across Application.OnKey. It did as expected but the after result was not good.

In my case how and where can I use the application.run command?

Regards,
Don
 
Last edited by a moderator:
Hmm, I may have misread your initial post. I'd still advise against using Application.OnKey as trigger.

At any rate. To deactivate Application.OnKey when workbooks with code isn't active. You'd add empty (i.e. without module string), Application.OnKey in Workbook_Deactivate event.

Code:
Private Sub Workbook_DeActivate()
    Application.OnKey "{ENTER}"
End Sub
 
Hi ,

Since you have used the Application.OnKey statement in your Workbook_Open macro , I assume you wish to run the code in the ExecuteMacro procedure only once.

Thus , to ensure that the ENTER key functions normally thereafter , just use the code which Chihiro has mentioned to be used in the Workbook_DeActivate macro as the last line in the ExecuteMacro itself.

Narayan
 
Hmm, I may have misread your initial post. I'd still advise against using Application.OnKey as trigger.

At any rate. To deactivate Application.OnKey when workbooks with code isn't active. You'd add empty (i.e. without module string), Application.OnKey in Workbook_Deactivate event.

Code:
Private Sub Workbook_DeActivate()
    Application.OnKey "{ENTER}"
End Sub
`

Thank you Chihiro,

I will remember your advise. Will try it out on Deactivate and enable again on Activate and just see how it works.

On the other hand (by not using application.OnKey) will look out for options to see if this can be worked out in Worksheet SelectionChange.

Regards,
Don
 
Hi ,

Since you have used the Application.OnKey statement in your Workbook_Open macro , I assume you wish to run the code in the ExecuteMacro procedure only once.

Thus , to ensure that the ENTER key functions normally thereafter , just use the code which Chihiro has mentioned to be used in the Workbook_DeActivate macro as the last line in the ExecuteMacro itself.

Narayan

Thank you Narayan
 
Back
Top