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
 
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
it is not giving any action or message as I entered HCnumber with fewer numbers means 3 digits not 8 digits after the HC
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Ok then, how about this...

VBA Code:
Private Sub txtHc_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim resp As String
    If txtHc = "" Then Exit Sub
    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
Ok then, how about this...

VBA Code:
Private Sub txtHc_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim resp As String
    If txtHc = "" Then Exit Sub
    resp = UCase(txtHc.Text)
    If Not resp Like "HC" & "########" Then
        txtHc.Text = ""
        txtHc.SetFocus
        Cancel = True
        MsgBox "Invalid Entry"
    End If
End Sub
Great dear
actually, I want to erase the previous comment because i discovered the mistake
now only working if there is an entry inside the textbox
and if I close the form or transfer to another form thru the buttons will not give any msg

thank you
God bless you
 
Upvote 0
At this point I don't know what you want. If you want to list all your requirements in one post, then perhaps we can get this done. Please consider all your requirements and possible outcomes.
 
Upvote 0
At this point I don't know what you want. If you want to list all your requirements in one post, then perhaps we can get this done. Please consider all your requirements and possible outcomes.
thank you, dear
it is working perfectly
just I have given you the feedback
my warm regrads
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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