ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Validating number of characters in textbox (https://www.excelbanter.com/excel-programming/361059-validating-number-characters-textbox.html)

Blondegirl[_9_]

Validating number of characters in textbox
 

Hi. I want to validate the data entry on a textbox so that a message box
appears if the user inputs a reference less than 7 digits long. (part of
a form which adds a new record to the sheet) I've set the textbox to
maxlength of 7 but can't get the code right. Please could someone
assist. Here is what I have so far which doesn't work:

If not TextBox1.MaxLength Then
MsgBox "The entry has to be 7 digits long"
TextBox1.SetFocus
Exit Sub
End If

Thank you.


--
Blondegirl
------------------------------------------------------------------------
Blondegirl's Profile: http://www.excelforum.com/member.php...o&userid=29615
View this thread: http://www.excelforum.com/showthread...hreadid=540634


mudraker[_386_]

Validating number of characters in textbox
 

Try

If not len(TextBox1) = 7 Then
MsgBox "The entry has to be 7 digits long"
TextBox1.SetFocus
Exit Sub
End If


--
mudraker
------------------------------------------------------------------------
mudraker's Profile: http://www.excelforum.com/member.php...fo&userid=2473
View this thread: http://www.excelforum.com/showthread...hreadid=540634


[email protected]

Validating number of characters in textbox
 
Do it on the EXIT event of textbox1 as follows

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1) < 7 Then
MsgBox "Text must be 7 characters long"
Cancel = True
End If
End Sub



All times are GMT +1. The time now is 08:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com