Validate a TextBox entry is a number of chars?

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
I've a Userform with a Textbox in it for users to enter a reference the number. The reference number must always be 10 digits in length, so my question is how can I make sure that it checks that the number of digits entered are correct?

I'm guessing that the validation will go inside

Code:
Private Sub TextBox1_Change()

End Sub

But beyond that I'm stumped.

Sincerely,
WageSlave101
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
one of the textbox's properties is maxlength. you just change that to 10

is this what you mean?

just click on the text box on your userform in VBE and on the left side there are properties

or do you want to validate whether numbers were entered and whether it is not less than 10?
 
Upvote 0
if you have a button put this line in it

Code:
Private Sub CommandButton1_Click()
If UserForm1.TextBox1.TextLength < 10 Then
MsgBox "Please enter 10"
Exit Sub
Else
MsgBox "coolio!"
End If
End Sub
 
Upvote 0
If you want to use Change event then
Code:
Private Sub TextBox1_Change()
With CreateObject("VBScript.RegExp")
     .Pattern = "\D+"
     .Global = True
     If Not .test(TextBox1.Value) Then Exit Sub
     TextBox1.Value = .replace(TextBox1.Value, "")
End With
End Sub

You could also use KeyPress event
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
     Case vbKey0 To vbKey9
     Case Else : KeyAscii = 0
End Select
End Sub
 
Upvote 0
i would actually do these 3 things..

1.) set the textbox to 10

2.) use jindons code to not allow users to enter non numeric

3.) button with my code to verify 10 digits
 
Upvote 0
10 degits
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Value) <> 10 Then
     MsgBox "10 degits entry only"
     Cancel = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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