Hello see if this helps,
Private Sub TextBox1_Enter()
' reset the values if you have to go back and modify it
Me.TextBox1.Text = Replace(Me.TextBox1.Text, " ", "")
Me.TextBox1.MaxLength = 6
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim strZip As String
strZip = UCase(Me.TextBox1.Text)
'Check if its a valid zipcode M1M 1M1 Format
If ValidZip(strZip) = True Then
'Change the max to 7 we need the space
Me.TextBox1.MaxLength = 7
' Split the string and add the space,
' place it back in the textbox
strZip = Left(strZip, 3) & " " & Right(strZip, 3)
Me.TextBox1.Text = strZip
ElseIf strZip = vbNullString Then
Exit Sub
Else
MsgBox "Invalid Zip format. Try it again, the middle space will be added automatically", _
vbInformation + vbOKOnly, "Invalid Zip"
End If
End Sub
Private Sub UserForm_Initialize()
TextBox1.MaxLength = 6
End Sub
Private Function ValidZip(strCode As String) As Boolean
If UCase(strCode) Like "[A-Z]#[A-Z]#[A-Z]#" Then
ValidZip = True
Else
ValidZip = False
End If
End Function
Thanks
Bookmarks