Run Macro on Keypress

GaryStone10

Board Regular
Joined
Nov 18, 2008
Messages
100
Hi all

This seems like it should be simple, but my google searching skills might be letting me down.

I want to have some code on a sheet so that whenever a user presses the up or down buttons it runs a macro.

Is this possible, and if so, how?

Many thanks in advance.
Gary Stone.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What about left or right?

There is a Worksheet_SelectionChange event macro that is triggered every time you change cells, so putting your code into that event might do the trick for you?
 
Upvote 0
Try:

Code:
Sub SetKey()
    Application.OnKey "{UP}", "YourMacro"
End Sub
 
Sub CancelKey()
    Application.OnKey "{UP}"
End Sub

Change YourMacro to the name of your macro and run the SetKey procedure. Run the CancelKey procedure to return to normal behaviour.
 
Upvote 0
Try:

Code:
Sub SetKey()
    Application.OnKey "{UP}", "YourMacro"
End Sub
 
Sub CancelKey()
    Application.OnKey "{UP}"
End Sub

Change YourMacro to the name of your macro and run the SetKey procedure. Run the CancelKey procedure to return to normal behaviour.

That works fine, many thanks.
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,290
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top