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, I just realized that you're talking about textbox on a userform, so use this one:
VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

With TextBox1
    If .Value <> "" Then
        If Not .Value Like "[A-Z][A-Z][A-Z][A-Z]0[A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]" Then
'            .Value = ""
             MsgBox "Wrong input", vbExclamation, "Incorrect Mobile Number"
            Cancel = True

        End If
    End If
End With


End Sub
Hi Akuini,

Sorry to ask a silly question, is it possible to check special characters when moving the focus out of the textbox then the focus will get back to the textbox along with the message.

really appreciate your help on this.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
but textbox should not accept any symbol
At first the code will accept any symbol, but after you try to exit the textbox, the message box will pop up, because the text isn't valid.
But if what you mean is preventing it each time you type a character then we would go back using Change event which as I said it's harder to code & also slower.
 
Upvote 0
At first the code will accept any symbol, but after you try to exit the textbox, the message box will pop up, because the text isn't valid.
But if what you mean is preventing it each time you type a character then we would go back using Change event which as I said it's harder to code & also slower.
Yes actually, I think my previous message was not cleared my request.

I am using the below change event to prevent Symbol from textbox, I have tried to use it under Exit event but it is not working (also I am not getting any error).

Could you please help me with it?

VBA Code:
Private Sub TxtName_Change()

Dim LastPosition As Long

Const PatternFilter As String = "*[!0-9A-Za-z ]*"

  Static LastText As String
  Static SecondTime As Boolean
  If Not SecondTime Then
    With txtName
     If .Text Like PatternFilter Then
        MsgBox "Special Character is not allowed in this tab", vbExclamation, "Special Character Found"
        SecondTime = True
        .Text = LastText
        .SelStart = LastPosition
      Else
        LastText = .Text
      End If
    End With
  End If
  SecondTime = False
  
End Sub
 
Upvote 0
I need to be clear about this:
Using my code in post #9:
At first the code will accept any symbol, but after you try to exit the textbox, the message box will pop up, because the text isn't valid.
Are you saying it doesn't work as I described OR it works as I described but it's not what you want, because what you want is to validate it every time you type a character?
 
Upvote 0
I need to be clear about this:
Using my code in post #9:
At first the code will accept any symbol, but after you try to exit the textbox, the message box will pop up, because the text isn't valid.
Are you saying it doesn't work as I described OR it works as I described but it's not what you want, because what you want is to validate it every time you type a character?
Previous code is working fine, actually I want another one code.
Below is the requirements.
User can put any character excluding Symbol. (there is no character limit for textbox)
I'm using below change event to prevent this. I have tried to use it under Exit but it's not working.
Could you please help me to validate this under Exit event.

VBA Code:
Private Sub TxtName_Change()

Dim LastPosition As Long

Const PatternFilter As String = "*[!0-9A-Za-z ]*"

  Static LastText As String
  Static SecondTime As Boolean
  If Not SecondTime Then
    With txtName
     If .Text Like PatternFilter Then
        MsgBox "Special Character is not allowed in this tab", vbExclamation, "Special Character Found"
        SecondTime = True
        .Text = LastText
        .SelStart = LastPosition
      Else
        LastText = .Text
      End If
    End With
  End If
  SecondTime = False
  
End Sub
 
Upvote 0
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
 
  • Like
Reactions: Zot
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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