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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,079
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
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?
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,079
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
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
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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
 

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
Thank you very much for the responses :biggrin:

I'll have a play with each of these

Thanks!
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,079
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
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
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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
 

Forum statistics

Threads
1,181,647
Messages
5,931,210
Members
436,784
Latest member
amuljono

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
Top