Results 1 to 3 of 3

Thread: [RESOLVED] How to use keypress in Excel VBA

  1. #1

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Resolved [RESOLVED] How to use keypress in Excel VBA

    hi everyone,

    I have a problem in using keypress event in excel. i have this code but it doesn't work. What i want to do is after I input a value in the textbox and press then Enter key the Commandbutton2_click will activate. Can anyone help me.

    Thanks and Regards,
    Kevin

    Code:
    Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 13 Then
    CommandButton2_Click
    End If
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to use keypress in Excel VBA

    enterkeys are not trappable in keypress event in msforms2 textboxes, use keydown or keyup event
    vb Code:
    1. Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    2. If KeyCode = 13 Then
    3. CommandButton1_Click
    4. End If
    5. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: How to use keypress in Excel VBA

    Hi pete,

    Your code works!! thanks!! now my only problem is how to trap if the user inputs an incorrect password. im still trying to figure out.
    Last edited by allankevin; Jul 30th, 2008 at 08:59 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width