Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 2 of 2

Thread: Only numbers in a textbox on a userform

  1. #1
    New Member
    Join Date
    Mar 2002
    Posts
    20
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I have a text box on a user form how do I make sure the only type of text that can be entered is numerical?

  2. #2
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I created a userform with Textbox1 on it, I then created code like this to put in the KeyPress event:


    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii

    Case Is < 46
    KeyAscii = 8 'character is printed, but we are sending backspace right immediately.
    Beep

    Case Is > 59
    KeyAscii = 8 'character is printed, but we are sending backspace right immediately.
    Beep

    Case 47 'Backslash character.
    KeyAscii = 8 'character is printed, but we are sending backspace right immediately.
    Beep

    End Select

    End Sub


    If you copy and paste this onto your userform, just remember to change "TextBox1" to whatever your text box is called.

    This allows only "1234567890" and "." to be entered into the text box.

    HTH

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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