View Single Post
  #16   Report Post  
Posted to microsoft.public.excel.misc
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default user form - how to set it up so it stores data when you press

The only thing I don't fully understand is the
if keycode is not equal to 13 then end sub.


Keycode 13 is both enter keys (keyboard and numeric keypad) so if you press
any other key then line below treminates the subroutine and allows input into
the textbox to continue.

If KeyCode < 13 Then Exit Sub

It then follows that if keycode = 13 the rest of the code in the sub executes.

Mike


"NDBC" wrote:

Thank you all for your help. I definitely need to learn vba. I didn't have
another _keydown but there was some other code in the worsheet sheet1.
Deleted it and all works fine. The only thing I don't fully understand is the
if keycode is not equal to 13 then end sub.

My take on it was if keycode is not 13 (which I assumed meant enter key)
then exit form. In practice I can press any key and nothing happens so I must
be wrong. Could you please help with the explanation.

Thanks again all three of you

"JLatham" wrote:

His code pretty much works for me. I took it and modified it somewhat,
including the "clear and reset" for TextBox1:

Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode < 13 Then
Exit Sub
End If
'put result at bottom of column A on active sheet
Cells(Cells. _
Rows.Count, "A").End(xlUp).Offset _
(1, 0) = TextBox1.Text
'clear/reset the Text Box
TextBox1 = ""
End Sub

Make sure you have the code in the right place: open your project, view the
UserForm object and double-click the textbox. It should show you the
_KeyDown event code you have. If that works, then double check to make sure
you don't have it declared more than once, perhaps elsewhere in your project.

If it doesn't show your code, it should at least create a stub for the
textbox's _Click event and you can use the right-hand pull-down to create a
stub for the _KeyDown event and just put the meat of the code into it and
give it a try. In this case, you probably definitely have another _KeyDown
event code laying around somewhere in the wrong place and need to get rid of
it.

"NDBC" wrote:

Mike,

Thanks for that. It seems like it is exactly what I'm looking for. I tried
copying it in but get this error "Procedure declaration does not match
description of event or procedure having same name". The textbox is called
textbox1 so it is not the name. Is there something else I need.

Thanks


"Mike H" wrote:

You could do it like this which detects the enter key being pressed

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode < 13 Then Exit Sub
mysheet = "Sheet1"
lastrow = Sheets(mysheet).Cells(Cells.Rows.Count, "A").End(xlUp).Row + 1
Cells(lastrow, 1).Value = TextBox1.Text
End Sub

Mike

"NDBC" wrote:

Thanks for the replys but neither are quite what I wanted. I can do what both
examples do. That is put in the text box and have a button that does the
command to store the entered data.

What a want is a form with only the text box so that when you hit enter the
data is stored. I don't want to have to either click on the button or press
enter twice.

Can this be done.