Consulting

Results 1 to 8 of 8

Thread: Multiple textboxes that only accepts numeric value

  1. #1
    VBAX Regular
    Joined
    Jan 2021
    Posts
    14
    Location

    Multiple textboxes that only accepts numeric value

    Hello! I just want to ask, is there a way for me to have multiple text boxes that only accept numeric value?

    So, the picture below is from my UserForm

    Capture.JPG

    This is the code I use for validation in which I must replicate per textbox.

    Private Sub tb_mtb_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        If Not IsNumeric(tb_mtb.Value) Then
            MsgBox "Only numbers allowed!", vbOKOnly + vbCritical, "Title"
            tb_mtb.Value = ""
        End If
    End Sub
    
    Private Sub tb_fil_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        If Not IsNumeric(tb_fil.Value) Then
            MsgBox "Only numbers allowed!", vbOKOnly + vbCritical, "Title"
            tb_fil.Value = ""
        End If
    End Sub

  2. #2
    VBAX Newbie
    Joined
    Jan 2021
    Posts
    4
    Location
    Hello,

    yes you can this is code i use in my userforms for numbers:

    Private Sub txtPart_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        'Case Asc("-") - u can add these if you need negative numbers
        'Case Asc(".") - u can add these if you need decimal numbers
        Case Else
            KeyAscii = 0
    End Select
    End Sub
    KeyAscii = 0 this is used to not allow spaces.

  3. #3
    VBAX Regular
    Joined
    Jan 2021
    Posts
    14
    Location
    Quote Originally Posted by iivansi2 View Post
    Hello,

    yes you can this is code i use in my userforms for numbers:

    Private Sub txtPart_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case Asc("0") To Asc("9")
        'Case Asc("-") - u can add these if you need negative numbers
        'Case Asc(".") - u can add these if you need decimal numbers
        Case Else
            KeyAscii = 0
    End Select
    End Sub
    KeyAscii = 0 this is used to not allow spaces.
    How can I apply this on my textboxes?

  4. #4
    VBAX Newbie
    Joined
    Jan 2021
    Posts
    4
    Location
    Your textboxes in your userform each of them have name when you using this vb code in your userform you have to change "txtPart" with name of your textbox where u need to use numbers.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    Is there a limited set of valid numbers ?

  6. #6
    VBAX Regular
    Joined
    Jan 2021
    Posts
    14
    Location
    Quote Originally Posted by iivansi2 View Post
    Your textboxes in your userform each of them have name when you using this vb code in your userform you have to change "txtPart" with name of your textbox where u need to use numbers.
    So I'm going to create a private sub with each of every textboxes, is that right?

  7. #7
    VBAX Regular
    Joined
    Jan 2021
    Posts
    14
    Location
    Quote Originally Posted by snb View Post
    Is there a limited set of valid numbers ?
    What do you mean by that? The value that should be in the textboxes is between 50-100.

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    In that case you'd better use a combobox filled with valid numbers:

    Combobox1.list=[row(50:100)]

Tags for this Thread

Posting Permissions

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