OnKey not working in Userform

AmateurExcelUser

New Member
Joined
Apr 7, 2014
Messages
14
I have set up a Userform in Excel 2013 with lots of Toggle and Command Buttons. I want to be able to set up some Function Keys (eg F1 to F5) that run some of the same procedures called by the Command buttons, so that the user has a choice of either clicking a Command button or pressing F2 (for example). However, I cannot get the OnKey event to work. As a simple test, I have set up the code blow in module1. The Userform (which works) is called by a separate procedure in the same Module1 using .Show. When I run this, the MsgBox does not pop up when I hit F2 from the shown Userform.

Thanks very much in anticipation for any help!


Sub SetUp_OnKey()
Application.Onkey "{F2}", "F2_Sub"
End Sub

Sub F2_Sub()
Msgbox "F2 pressed" 'obviously when I get this working I can run a sub procedure here
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I am pretty sure that the OnKey is only going to work when the workbook or part of the normal application has the focus. Not when a userform has the focus.
 
Upvote 0
Re: Making clever hotkeys in a Userform which contain Toggles and Command Buttons

Thanks - what a shame.

What I want to do instead is have a number of toggle and command buttons for various selections in a userform, each of which are labelled with both a name and a unique letter as an identifying key. I want to allow the user the choice of either clicking on any button (either to make a toggle selection or to run a procedure as a click_event for a command button) but also to enable the user to enter one of the identifying single letters in a textbox which, if valid, would either make the relevant toggle change or start a procedure as if one of the buttons had been clicked. This would allow the user simply to enter a single letter as a hotkey without needing to press alt/ctrl or pressing enter. I imagine the textbox (which could be made invisible) would have to be kept in focus but the toggle/command buttons must still be triggered if they are clicked instead of a letter being entered. If it is a toggle request, the textbox must then be set back into focus. A single letter pressed in the textbox must either trigger an event or do nothing if an invalid letter.

I reckon I can handle the coding once I have understood the principles and structure.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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