Excel UserForm TextBox Character Limit Must Equal 0 or 8

Nadine67

Board Regular
Joined
May 27, 2015
Messages
225
Hello and thank you for any attention this post may receive.

So I have a userform (frmInput) which has 25 texboxes (TextBox1 to TextBox25) and all of which have the 'MaxLength' set to 8, and restricted to numbers only. However this still allows me to input any length of characters from 1 to 8. I would like all 25 textboxes to only accept blank or 8char. Not sure how to do this. I have looked online but can only really find a character limit which is not exactly what I am after.

Any thoughts would be greatly appreciated.

This is the code (I found on www) to limit textbox input to numbers only.
Code:
Private Sub InputTextBox_keypress _
(ByVal KeyAscii As msforms.ReturnInteger)
'Allows only numbers.
'Dim sText
Select Case KeyAscii
   Case 47 To 57
   Case Else
      KeyAscii = 0
End Select
'Save the text in a variable
sText = InputTextBox.Text
End Sub

Cheers and have a great day!
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Maybe something like this - add a message box or clear the text box at the line 'do something
Rich (BB code):
Private Sub InputTextBox_keypress _
(ByVal KeyAscii As msforms.ReturnInteger)
'Allows only numbers.
'Dim sText
Select Case KeyAscii
   Case 47 To 57
   Case Else
      KeyAscii = 0
End Select
'Save the text in a variable
stext = InputTextBox.Text
If stext <> "" Then
    If Len(stext) <> 8 Then
        'do something
    End If
End If
End Sub
 
Upvote 0
Thank you JoeMo.

I have used your suggestion and have a MsgBox indicating the error. However is it possible to stop the user from moving away from the textbox or form if any of the 25 textboxes do not meet the criteria of blank or 8char?
 
Upvote 0
JoeMo this is what I have and it allows me to enter 1 character before the msgbox appears. I am not able to enter >1 character. Eeeek
Code:
Private Sub InputTextBox_keypress _
(ByVal KeyAscii As msforms.ReturnInteger)
'Allows only numbers.
'Dim sText
Select Case KeyAscii
   Case 47 To 57
   Case Else
      KeyAscii = 0
End Select
'Save the text in a variable
stext = InputTextBox.Text

If stext <> "" Then
    If Len(stext) <> 8 Then
        MsgBox "The workorder must be an 8 digit number.  Please enter the correct 8 digit number."
    End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,196,487
Messages
6,015,493
Members
441,898
Latest member
kofafa

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