I am new to VBA. I am constructing a VBA userform. I want to restrict input into one of the textboxes to either a 16 or 21 character string with only numerals or spaces (the spaces are to be entered automatically). The first 5 digits are static for both formats, i.e.
format for 16 digit is "84271 ##### ####"
format for 21 digit is "84271 ##### #### ####"
I have created the following code:
Private Sub expresscode_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Let expresscode.Text = Format$(expresscode.Text, "84271 ##### #### ####")
If Len(expresscode.Value) = 21 Then
Exit Sub
Let expresscode.Text = Format$(expresscode.Text, "84271 ##### ####")
ElseIf Len(expresscode.Value) = 16 Then
Exit Sub
Else
MsgBox "Please enter a 9 or 13 digit number"
Cancel = True
End If
End Sub
This tests and works correctly for the 21 character input. It is not reading the 16 character format as when I enter 9 numerals, I get the following:
84271 1 2345 6789
and my message box appears asking for a 9 or 13 digit input.
Any help would be appreciated.
Thank you.
Bookmarks