The first 4 characters can be any alphabet (A-Z) and the fifth one should be zero ( 0 ) or any numeric value in the textbox of user form.

SamarthSalunkhe

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

I am using the below code to restrict numbers only in the textbox of user form, it is working very well. just for knowledge purposes, I want to change restrictions like in the test box I want to allow the first 4 characters can be any alphabet (A-Z) and the fifth one should be zero ( 0 ) or any numeric value. Eg. : "ABCD0BD459"

VBA Code:
Private Sub txtMobile_Change()
 'Mobile number Must be in Numbers only.
Dim x As Long
Dim a As String
a = "##########"

With txtMobile
    x = Len(.Text)
    If Not .Text Like Left(a, x) Then
          .Text = Left(.Text, x - 1): MsgBox "Mobile number Must be Numeric", vbExclamation, "Incorrect Mobile Number"
    End If
End With
End Sub
 
Sorry, we should add " Cancel = True"
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Const PatternFilter As String = "*[!0-9A-Za-z ]*"
    With TextBox1
     If .Text Like PatternFilter Then
        MsgBox "Special Character is not allowed in this tab", vbExclamation, "Special Character Found"
        Cancel = True
      End If
    End With

End Sub
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try:
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Const PatternFilter As String = "*[!0-9A-Za-z ]*"
    With TextBox1
     If .Text Like PatternFilter Then
        MsgBox "Special Character is not allowed in this tab", vbExclamation, "Special Character Found"

      End If
    End With

End Sub
Thank you so much, for your support.

and sorry to disturb you again and again.
 
Upvote 0
You should use the code from post #21, I added " Cancel = True"
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,216,146
Messages
6,129,134
Members
449,488
Latest member
qh017

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