Activating a command button with a single key stroke

B

Bishop

Is there a way to activate a command button by simply pressing a single
letter on the keyboard? For example, say I have a button that says "Add A
Title". When I press the A key I want the button to activate and process
whatever code is attached to it.
 
J

Jacob Skaria

Use the keydown event of the command button. or the userform..

Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
If KeyCode = 65 Then MsgBox "Run"
End Sub
 
D

Dave Peterson

You could use the Accelerator property, but then use alt-A to run the _click
event for that command button. (Include the A somewhere in the caption!)

Kind of like what you see in the xl2003 menu--where the F in File is underlined,
E in Edit, etc.
 
P

Patrick Molloy

read help on
APPLICATION.ONKEY key, procedure

when this is run
APPLICATION.ONKEY "Q", "doThis"

whenever the user presses the Q key, the sub called doThis is run
 
B

Bishop

I have the following code:

Private Sub AddButton_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

Dim A As Integer

If KeyCode = A Then
AddTitle.Show
End If

End Sub

but it doesn't work. What am I doing wrong?
 
J

Jacob Skaria

Its returning the character code..65 for A and so on..However if you are
unfamilar use the below code....

Dim A As Integer

If Chr(KeyCode) = "A" Then
AddTitle.Show
End If

If this post helps click Yes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top