Number restriction in VBA UserForm

SamarthSalunkhe

Board Regular
Joined
Jun 14, 2021
Messages
103
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone,

I'm using the below code for restriction in UserForm as the user must enter a number only.

but on the advanced level, I want the first 2 digits should start with 27 or 99.

is it possible to add this restriction in the below code?


VBA Code:
Private Sub txtTIN_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case Len(Me.txtTIN.Text)
    Case 0
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "First Digit should be Numeric", vbExclamation, "Incorrect TAN"
       
        End If
     Case 1
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Second Digit should be Numeric", vbExclamation, "Incorrect TAN"
       
       
        End If
     Case 2
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Third Digit should be Numeric", vbExclamation, "Incorrect TAN"
       
        End If
     Case 3
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Fourth Digit should be Numeric", vbExclamation, "Incorrect TAN"
       
        End If
     Case 4
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Fifth Digit should be Numeric", vbExclamation, "Incorrect TAN"
               
        End If
     Case 5
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Sixth Digit should be Numeric", vbExclamation, "Incorrect TAN"
       
        End If
     Case 6
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Seventh Digit should be Numeric", vbExclamation, "Incorrect TAN"
       
        End If
     Case 7
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Eighth Digit should be Numeric", vbExclamation, "Incorrect TAN"
       
        End If
     Case 8
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Ninth Digit should be Numeric", vbExclamation, "Incorrect TAN"
           
        End If
     Case 9
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Tenth Digit should be Numeric", vbExclamation, "Incorrect TAN"
           
        End If
     Case 10
        If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 0
            MsgBox "Eleventh Digit should be Numeric", vbExclamation, "Incorrect TAN"
           
        End If
     Case 11
        If (KeyAscii > 64 And KeyAscii < 91) Or KeyAscii = 8 Then
            KeyAscii = KeyAscii
        Else
            KeyAscii = 80
            MsgBox "Twelfth Digit Must be { P } in UPPERCASE Only", vbExclamation, "Incorrect TAN"
           
           
    End If
End Select

End Sub

Thank you,
Samarth
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi, @SamarthSalunkhe
Try:
VBA Code:
Private Sub TextBox1_Change()
' start with "27" or "99" & followed by unspecified digit
Dim x As Long
Dim a As String, b As String
a = "27"
b = "99"
    With TextBox1
        x = Len(.Text)
        Select Case x
            Case 1 To 2
                If Not .Text Like Left(a, x) And Not .Text Like Left(b, x) Then Beep: .Text = Left(.Text, x - 1)
            Case Is > 2
                If Not .Text Like Left(a & WorksheetFunction.Rept("#", x - 2), x) And Not .Text Like _
                Left(b & WorksheetFunction.Rept("#", x - 2), x) Then Beep: .Text = Left(.Text, x - 1)
        End Select
    End With
End Sub
 
Upvote 0
MsgBox "Twelfth Digit Must be { P } in UPPERCASE Only", vbExclamation, "Incorrect TAN"

I just realized that you want to include letter "P" as the 12th char.
I also just realized that I made my previous code too complicated.:cry:
VBA Code:
Private Sub TextBox1_Change()
' start with "27" or "99" & followed by 9 digit & "P"
Dim x As Long
Dim a As String, b As String
a = "27#########P"
b = "99#########P"
With TextBox1
    x = Len(.Text)
    If Not .Text Like Left(a, x) And Not .Text Like Left(b, x) Then
          .Text = Left(.Text, x - 1): Beep
    End If
End With
End Sub
 
Last edited:
Upvote 0
Solution
I just realized that you want to include letter "P" as the 12th char.
I also just realized that I made my previous code too complicated.:cry:
VBA Code:
Private Sub TextBox1_Change()
' start with "27" or "99" & followed by 9 digit & "P"
Dim x As Long
Dim a As String, b As String
a = "27#########P"
b = "99#########P"
With TextBox1
    x = Len(.Text)
    If Not .Text Like Left(a, x) And Not .Text Like Left(b, x) Then
          .Text = Left(.Text, x - 1): Beep
    End If
End With
End Sub
Hello Akuini,

Thank you so much for your solution, It is working very well.
 
Upvote 0
@Akuini why you don't use for dimension as variant not string you have numbers and letters . why use as string .

I tested your code I can't write the letter P in the end as your code . if I'm wrong please correct me. I would just understand

thanks
 
Upvote 0
Thank you so much for your solution, It is working very well.
Glad it works.
If you want a message box, how about a more generic message like this:
VBA Code:
Private Sub TextBox1_Change()
' start with "27" or "99" & followed 9 digit & "P"
Dim x As Long
Dim a As String, b As String
a = "27#########P"
b = "99#########P"
With TextBox1
    x = Len(.Text)
    If x = 0 Then Exit Sub
    If Not .Text Like Left(a, x) And Not .Text Like Left(b, x) Then
        tx = "The format must be (x is any digit):" & vbLf & _
        "27xxxxxxxxxP OR 99xxxxxxxxxP"
        MsgBox tx, vbExclamation, "Incorrect TAN"
        .Text = Left(.Text, x - 1)
    End If
End With
End Sub
 
Upvote 0
why you don't use for dimension as variant not string you have numbers and letters . why use as string .
In this article:
it says:
pattern: Required; any string expression conforming to the pattern-matching conventions described in Remarks.

The pattern is a string even if it has number in it. So I use string variable not variant, but variant should work too.

I tested your code I can't write the letter P in the end as your code
Not sure why that happens. Try a simpler pattern, like:
"2#P"
see if it works.
 
Upvote 0
Just curious, if 12th char is always/must be a P, why bother with entering it in the first place? :)
 
Upvote 0
Glad it works.
If you want a message box, how about a more generic message like this:
VBA Code:
Private Sub TextBox1_Change()
' start with "27" or "99" & followed 9 digit & "P"
Dim x As Long
Dim a As String, b As String
a = "27#########P"
b = "99#########P"
With TextBox1
    x = Len(.Text)
    If x = 0 Then Exit Sub
    If Not .Text Like Left(a, x) And Not .Text Like Left(b, x) Then
        tx = "The format must be (x is any digit):" & vbLf & _
        "27xxxxxxxxxP OR 99xxxxxxxxxP"
        MsgBox tx, vbExclamation, "Incorrect TAN"
        .Text = Left(.Text, x - 1)
    End If
End With
End Sub
Hi Akuini,

I have used MsgBox in place of Beep, and that one also works perfectly.

Thank you ?

VBA Code:
Private Sub txtTIN_Change()
' start with "27" or "99" & followed by 9 digit & "P"
Dim x As Long
Dim a As String, b As String
a = "27#########P"
b = "99#########P"
With txtTIN
    x = Len(.Text)
    If Not .Text Like Left(a, x) And Not .Text Like Left(b, x) Then
          .Text = Left(.Text, x - 1): MsgBox "First 2 Digit of TIN must be 27 or 99", vbExclamation, "Incorrect TAN"
    End If
End With
End Sub
 
Upvote 0
The pattern is a string even if it has number in it. So I use string variable not variant, but variant should work too.
thanks for clarification
Not sure why that happens. Try a simpler pattern, like:
"2#P"
see if it works.
unfortunately doesn't work . anyway thanks for your time
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,533
Members
452,652
Latest member
eduedu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top