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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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