INKEY and VBA

J

John Hayes

Hi,

Is there a VBA equivalent to INKEY to get input from the
keyboard?

Regards

John
 
J

Jake Marx

Hi John,

You're probably looking for InputBox:

Dim sAnswer As String

sAnswer = Application.InputBox("Please enter text:","TEST","Default")

MsgBox sAnswer
 
Joined
Feb 10, 2012
Messages
2
Reaction score
0
Here is one way to set up a substitute for INKEY$

You add a userform to your sheet. When the form is SHOWing there are calls like the Trials below which detect the event of a keypress.

You display the form with the following sub

Sub Macro1
Userform1.Show
End sub


'TRIAL 1
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'When a key is pressed while the UserForm is showing, the key is passed to the variable KeyAscii
'You can display the key with the following step
MsgBox Chr(KeyAscii)
‘You can also put in some code here to perform or call another routine
'To avoid an infinite loop, you have to hide the form
UserForm1.Hide
End Sub

‘TRIAL 2
‘When you get better at using this routine, you can leave the form open and have
‘it close when a certain key is pressed, like “Q”, for Quit

Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'When a key is pressed while the UserForm is showing, the key is passed to the variable KeyAscii
'You can display the key with the following step
MsgBox Chr(KeyAscii)
'To avoid an infinite loop, you have to hide the form
UserForm1.Hide
End Sub

Finally, as a failsafe, to avoid an infinite loop, you can add a command button to your form which when clicked, will hide your form.
 

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

Similar Threads


Top