+ Reply to Thread
Results 1 to 10 of 10

Help With Canadian Postal Code In Userform

Hybrid View

  1. #1
    Registered User
    Join Date
    12-02-2013
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    89

    Help With Canadian Postal Code In Userform

    Hello All,

    I have a Useform with a textbox where I would like to input the Canadian Postal Code with this format XXX XXX, Please notice there is an space in the middle. How could I input caracters and the checkbox recognize it has to put an space in the middle and only able to type 6 caracters (letters and numbers)

    Thank you in advance

    Regards,
    Last edited by eddyrcabrera79; 12-04-2013 at 03:00 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Help With Canadian Postal Code In Userform

    Hello eddycabrera79,

    This example assumes the name of the TextBox is "TextBox1". This limits the user input to six characters and then inserts a space.

    UserForm Code
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        TextBox1.Text = Left(TextBox1.Text, 3) & " " & Right(TextBox1.Text, 3)
    End Sub
    
    Private Sub UserForm_Initialize()
        TextBox1.MaxLength = 6
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-02-2013
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Help With Canadian Postal Code In Userform

    Thank you Leith
    This code worked fine for me

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox1.Text = Left(TextBox1.Text, 3) & " " & Right(TextBox1.Text, 3)
    End Sub

    Private Sub UserForm_Initialize()
    TextBox1.MaxLength = 6
    End Sub

    I forgot one thing, the Canadian Postal code is a combination of numbers and letters in this way M1M 1M1, Is there a way to resctirc the entry to this format?

  4. #4
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Help With Canadian Postal Code In Userform

    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
    
    ' Add value to variable anc change the max to 7
    ' we need the space
    strZip = Me.TextBox1.Text
    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
    
    End Sub
    This code is for a textbox1 modify to match your textbox name.

    Thanks

  5. #5
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Help With Canadian Postal Code In Userform

    By the way Leith just reminded me I forgot something. the initial value for the maxLength.

    Thanks Leith

    So this is my final code with everything:

    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
    
    ' Add value to variable anc change the max to 7
    ' we need the space
    strZip = Me.TextBox1.Text
    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
    
    End Sub
    
    Private Sub UserForm_Initialize()
        TextBox1.MaxLength = 6
    End Sub
    Thanks

  6. #6
    Registered User
    Join Date
    12-02-2013
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    89

    {Help With Canadian Postal Code In Userform

    Thank you Leith and Fredlo for your help.

    This code worked fine for me

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox1.Text = Left(TextBox1.Text, 3) & " " & Right(TextBox1.Text, 3)
    End Sub

    Private Sub UserForm_Initialize()
    TextBox1.MaxLength = 6
    End Sub

    I forgot one thing, the Canadian Postal code is a combination of numbers and letters in this way M1M 1M1, Is there a way to resctirc the entry to this format?
    Last edited by eddyrcabrera79; 12-04-2013 at 02:52 AM.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Help With Canadian Postal Code In Userform


  8. #8
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Help With Canadian Postal Code In Userform

    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

  9. #9
    Registered User
    Join Date
    12-02-2013
    Location
    Miami
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Help With Canadian Postal Code In Userform

    thank you so much

  10. #10
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Help With Canadian Postal Code In Userform

    You are welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Format Canadian Postal Code on Entry
    By Gos-C in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 03-24-2019, 04:20 PM
  2. Distance between Canadian Postal Codes
    By lesleyhollett in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-17-2013, 10:47 AM
  3. [SOLVED] Format for Canadian Postal Codes
    By rmcc in forum Excel General
    Replies: 25
    Last Post: 07-24-2012, 02:04 PM
  4. Adding spaces to Canadian postal code
    By loren.silverman in forum Excel General
    Replies: 3
    Last Post: 03-01-2012, 10:02 PM
  5. Using excel 2003 cannot see Canadian Postal Codes
    By PW in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-08-2005, 03:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1