Consulting

Results 1 to 10 of 10

Thread: Only numbers in a texbox

  1. #1
    VBAX Regular
    Joined
    Jan 2011
    Posts
    13
    Location

    Only numbers in a texbox

    hello,

    how can i force a user to complete in a textbox userform only numbers - no letters ?

    thank you

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I'm sure there's an easier way, but try:

    Private Sub TextBox1_Change()
    Static REX As Object
    If REX Is Nothing Then
                Set REX = CreateObject("VBScript.RegExp")
                With REX
            .Global = True
            .Pattern = "[^0-9]"
                End With
        End If
    TextBox1.Value = REX.Replace(TextBox1.Value, vbNullString)
    End Sub
    Hope that helps,

    Mark

  3. #3
    VBAX Regular
    Joined
    Jan 2011
    Posts
    13
    Location
    Thank you, Mark

    It`s exactly what I need!

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by mariusc
    Thank you, Mark

    It`s exactly what I need!
    Happy to help

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This is another way

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        KeyAscii = -KeyAscii * (Chr(KeyAscii) Like "[0-9]")
    End Sub

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Neat Mike! I didn't know you could use Chr(0) like that.

  7. #7
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    KeyAscii = 0 is not the same as Chr(0), its an internal number.
    It (and KeyCode from the KeyDown event) can be fun to work with.

    If you have two textboxes, this code will cause an X entered in TextBox1 to be automaticaly moved to TextBox2, along with the focus. The KeyAscii (and its associated effect on a textbox moves from TB1 to TB2, along with the focus.

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 
        If Chr(KeyAscii) = "X" Then
        TextBox2.SetFocus
        End If
    End Sub

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In a similar vein, the following code ensures only numbers can be input. Any non-numeric keystroke generates a beep and the offending character is deleted.
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not (KeyAscii > 47 And KeyAscii < 59) Then
     Beep
     KeyAscii = 0
    End If
    End Sub
    Or, if you want to allow the user to input decimal values:
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not (KeyAscii > 47 And KeyAscii < 59) Then
      If KeyAscii = 46 And Len(TextBox1.Text) = 0 Then
        TextBox1.Text = "0"
      ElseIf Not (KeyAscii = 46 And InStr(TextBox1.Text, ".") = 0) Then
        Beep
        KeyAscii = 0
      End If
    End If
    End Sub
    Note: With the second sub, only a single period can be input, and a leading 0 is automatically inserted if the period is the first character input by the user.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This will allow the entry of any number, including negatives. The newStr technique can be used in other situations to validate in the KeyPress event.
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Dim newStr As String
        With TextBox1
        newStr = Left(.Text, .SelStart) & Chr(KeyAscii) & Right(.Text, Len(.Text) - .SelStart - .SelLength)
        If Not IsNumeric(newStr & "0") Then
            KeyAscii = 0
        End If
        End With
    End Sub
    
    Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox1.Text = Val(Replace(TextBox1.Text, ",", vbNullString))
    End Sub
    The BeforeUpdate cleans up user entries like "1,1223.45". Used with the Format command, it allows you to force the entry into a particular format, even if the user enters a different format.

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by mikerickson
    KeyAscii = 0 is not the same as Chr(0), its an internal number.
    Quote Originally Posted by GTO
    Neat Mike! I didn't know you could use Chr(0) like that.
    Can we all pretend I didn't say that?

    Thank you again Mike. I missed seeing the paranthesis and have no idea what I was thinking. You were using the -1 produced if the Like resulted in True.

    Mark

Posting Permissions

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