Formatting a textbox to accept specific characters

Maher_Zeetawi

New Member
Joined
Sep 11, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
hello good day
can i get your help if can get code for Textbox need to accept this format of character For Example
HC00123456
HC05213141
Etc
only these two letters then followed by 8 numbers & if wrong to give massage upon pressing on the add button
hopefully can get the answer
my regards
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Assuming that this is for a UserForm, then how about this Sub placed in the UserForm Module. The first line of code will enable you to leave the field blank. Delete that line, if you do not want that option.
This code is for a TextBox named TextBox1

VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim resp As String
    If TextBox1 = "" Then Exit Sub      'Delete this line if an entry is mandatory
    resp = TextBox1.Text
    If Not resp Like "HC" & "########" Then
        TextBox1.Text = ""
        TextBox1.SetFocus
        Cancel = True
        MsgBox "Invalid Entry"
    End If
End Sub
 
Upvote 0
Assuming that this is for a UserForm, then how about this Sub placed in the UserForm Module. The first line of code will enable you to leave the field blank. Delete that line, if you do not want that option.
This code is for a TextBox named TextBox1

VBA Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim resp As String
    If TextBox1 = "" Then Exit Sub      'Delete this line if an entry is mandatory
    resp = TextBox1.Text
    If Not resp Like "HC" & "########" Then
        TextBox1.Text = ""
        TextBox1.SetFocus
        Cancel = True
        MsgBox "Invalid Entry"
    End If
End Sub
I applied it & worked but faced 2 issues
1. once I run the form give the msg box Invalid Entry
2. the hc lower case not working
and I have a question about the note you wrote 'Delete this line if an entry is mandatory?
actually, this HC number is mandatory entry and connected with the command button once empty it will not document and transfer the data into the sheets
 
Upvote 0
can i get your help if can get code for Textbox need to accept this format of character For Example
HC00123456
HC05213141
Etc
only these two letters then followed by 8 numbers & if wrong to give massage upon pressing on the add button

You were pretty specific in the wording and it would appear as if you wanted "HC" only. Try this. Additionally, now there must be the correct text format to exit the TextBox.

VBA Code:
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim resp As String
    resp = UCase(TextBox4.Text)
    If Not resp Like "HC" & "########" Then
        TextBox4.Text = ""
        TextBox4.SetFocus
        Cancel = True
        MsgBox "Invalid Entry"
    End If
End Sub
 
Upvote 0
You're welcome. I was happy to help. Thanks for the feedback.

Please mark the post that answered your question as the solution to help future readers. Little check mark icon right side of the post.
 
Upvote 0
hello
excuse me can I update
I faced problems
if I changed my mind and I don't want to fill the text box & want to transfer to another form or close the form without entry, it will show me the msgbox "invalid entry"
kindly see the attached screen shot
 

Attachments

  • screen shot wound form error.png
    screen shot wound form error.png
    45.9 KB · Views: 6
Upvote 0
Do you face the same problem if you use the solution in Post #2...
You must also change this line:
VBA Code:
resp = TextBox1.Text
to this
VBA Code:
resp = UCase(TextBox4.Text)
 
Upvote 0
Do you face the same problem if you use the solution in Post #2...
You must also change this line:
VBA Code:
resp = TextBox1.Text
to this
VBA Code:
resp = UCase(TextBox4.Text)
I did not get you
kindly which line to change because it is that same line you want me to change " resp = UCase(txtHc.Text)"
but I'm using this code and facing that problem I mentioned


Private Sub txtHc_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim resp As String
resp = UCase(txtHc.Text)
If Not resp Like "HC" & "########" Then
txtHc.Text = ""
txtHc.SetFocus
Cancel = True
MsgBox "Invalid Entry"
End If
End Sub
 
Upvote 0
Try this:

VBA Code:
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim resp As String
    If TextBox4 = "" Then Exit Sub
    resp = UCase(TextBox4.Text)
    If Not resp Like "HC" & "########" Then
        TextBox4.Text = ""
        TextBox4.SetFocus
        Cancel = True
        MsgBox "Invalid Entry"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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