Share via

vba code for arrow keys

Anonymous
2012-04-23T14:26:57+00:00

I have a line of code that needs to be ran when a user clicks one of the arrow keys.  Each key (up, dn, left, right) has it's own code.  Is there a way to do this?

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2012-04-23T14:48:27+00:00

Here is a (rather irritating) example of how to execute code when the user presses the right arrow key. Notice that it replaces the built-in action:

' Code to run

Sub Code4RightArrow()

    Beep

End Sub

' Set right arrow key to run Code4RightArrow

Sub AssignRightArrow()

    Application.OnKey "{RIGHT}", "Code4RightArrow"

End Sub

' Restore the built-in action of the right arrow key

Sub RestoreRightArrow()

    Application.OnKey "{RIGHT}"

End Sub

The "names" of the other arrow keys are {LEFT}, {UP} and {DOWN}.

Was this answer helpful?

5 people found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful