+ Reply to Thread
Results 1 to 27 of 27

Format Canadian Postal Code on Entry

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Question Format Canadian Postal Code on Entry

    Hi everyone,

    I can't find a custom format to validate a properly formatted Canadian postal code on entry, so I think my last hope is to ask someone to write me a VBA code.

    Canadian postal codes consist of six characters with a space in the middle: a capital letter, a number, a capital letter, a space, a number, a capital letter, and a number. Therefore, M2A 3J4 is a properly formated postal code.

    I want a code to fix an improperly fomatted postal code (such as M2A3J4 or m2a3j4 or m3a 2j4) on entry.

    Can anyone please help me?

    Thanks,
    Gos-C
    Using Excel 2010 & Windows 10
    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Gos-C

    Try this macro - It does not verify Letters & Number are placed in the correct order - This can be added if required.

    Copy this macro
    Goto Excel
    Right Click on Sheet Name Tab > select View Code
    Past macro into the Worksheet Module displayed

    Change Column number to suit (shown In red in this posting)
    Return to Excel and test


    Private Sub Worksheet_Change(ByVal Target As Range)
       Dim Rng As Range
       
       For Each Rng In Target
          If Rng.Column = 1 Then
             Application.EnableEvents = False
             Select Case Len(Rng.Value)
             Case 6
                Rng.Value = UCase(Left(Rng.Value, 3) & " " & Right(Rng.Value, 3))
             Case 7
                Rng.Value = UCase(Rng.Value)
             End Select
          End If
       Next Rng
       Application.EnableEvents = True
    End Sub

  3. #3
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi Mudraker,

    Thank you very much. It works but, if you don't mind, can you add the verification of the letters and numbers in the correct order. That would be perfect. I really appreciate your help.

    Thanks again,
    Gos-C

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Gos

    This version verifies letters, numbers, space in correct location.

    Change Column number to suit

    For invalid Postcode entries if you remove the ' before 'rng.ClearContents it will clear the entry


    Private Sub Worksheet_Change(ByVal Target As Range)
       Dim Rng As Range
       Dim iChar As Integer
       
       For Each Rng In Target
          If Rng.Column = 1 Then
             Application.EnableEvents = False
             Select Case Len(Rng.Value)
             Case 6
                Rng.Value = UCase(Left(Rng.Value, 3) & " " & Right(Rng.Value, 3))
             Case 7
                Rng.Value = UCase(Rng.Value)
             End Select
             If Len(Rng.Value) <> 7 Then
                MsgBox "Invalid Post Code Entyry in " & Rng.Address
                Exit For
             End If
             For iChar = 1 To 7 Step 1
                Select Case Mid(Rng.Value, iChar, 1)
                Case "A" To "Z"
                   Select Case iChar
                   Case 2, 4, 5, 7
                      MsgBox Rng.Address & " - " & Mid(Rng.Value, iChar, 1) _
                      & " is invalid for Postcode"
                      'rng.ClearContents
                      Exit For
                   End Select
                Case 0 To 9
                   Select Case Mid(Rng.Value, iChar, 1)
                   Case 1, 3, 4, 6
                      MsgBox Rng.Address & " - " & Mid(Rng.Value, iChar, 1) _
                      & " is invalid for Postcode"
                      'rng.ClearContents
                      Exit For
                   MsgBox Rng.Address & " - " & Mid(Rng.Value, iChar, 1) _
                      & " is invalid for Postcode"
                      'rng.ClearContents
                      Exit For
                   End Select
                Case " "
                   Select Case Mid(Rng.Value, iChar, 1)
                   Case 1 To 3, 5 To 7
                      MsgBox Rng.Address & " - " & Mid(Rng.Value, iChar, 1) _
                         & " is invalid for Postcode"
                      'rng.ClearContents
                      Exit For
                   End Select
                Case Else
                   MsgBox Rng.Address & " - " & Mid(Rng.Value, iChar, 1) _
                         & " is invalid for Postcode"
                      'rng.ClearContents
                   Exit For
                End Select
             Next iChar
          End If
       Next Rng
       Application.EnableEvents = True
    End Sub

  5. #5
    Forum Expert oldchippy's Avatar
    Join Date
    02-14-2005
    Location
    Worcester, UK
    MS-Off Ver
    Excel 2007 (Home)
    Posts
    7,097
    Here's an alternative if you require a formula, with your data entry in A1, then this in B1

    =LEFT(UPPER(A1),3)&" "&RIGHT(UPPER(A1),3)
    oldchippy
    -------------


    Blessed are those who can give without remembering and take without forgetting

    If you are happy with the help you have received, please click the <--- STAR icon on the left - Thanks.

    Click here >>> Top Excel links for beginners to Experts

    Forum Rules >>>Please don't forget to read these

  6. #6
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Hi Mudraker,

    Thanks again for taking time to help me. I sincerely appreciate it.

    When I enter an improperly formatted postal code -- e.g., a1b2c3 -- in cell A1, it gets formatted properly (A1B 2C3) but I get the message: $A$1 - 1 is invalid for Postcode. It happens all the time. Do I need to modify the code?

    Thanks,
    Gos-C

  7. #7
    Registered User
    Join Date
    02-19-2013
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2011
    Posts
    1

    Re: Format Canadian Postal Code on Entry

    Quote Originally Posted by mudraker View Post
    Gos-C

    Try this macro - It does not verify Letters & Number are placed in the correct order - This can be added if required.

    Copy this macro
    Goto Excel
    Right Click on Sheet Name Tab > select View Code
    Past macro into the Worksheet Module displayed

    Change Column number to suit (shown In red in this posting)
    Return to Excel and test


    Private Sub Worksheet_Change(ByVal Target As Range)
       Dim Rng As Range
       
       For Each Rng In Target
          If Rng.Column = 1 Then
             Application.EnableEvents = False
             Select Case Len(Rng.Value)
             Case 6
                Rng.Value = UCase(Left(Rng.Value, 3) & " " & Right(Rng.Value, 3))
             Case 7
                Rng.Value = UCase(Rng.Value)
             End Select
          End If
       Next Rng
       Application.EnableEvents = True
    End Sub
    Hi Mudraker,

    This work is fantastic and thank you in advance on behalf of all Canadian excel users who have been loosing sleep in trying to automate the post code entry/ format process!

    I am a Excel 2011 for Mac user and when I right click on the Sheet Tab, I do not have an option to View Code. I tried pasting the code into the macro record window but now every entry in the sheet comes up with an 'Invalid Postal Code' error.

    Could you please offer some (more!) guidance?

  8. #8
    Registered User
    Join Date
    02-27-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Format Canadian Postal Code on Entry

    Does anyone know if I can use this code to force postal code entry in a user form text box?

  9. #9
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    845

    Re: Format Canadian Postal Code on Entry

    Zheng,

    Welcome to the forum.
    If you are asking a question then you should start your own thread.
    Charles

    There are other ways to do this, this is but 1 !
    Be Sure you thank those who helped.
    IF YOU'RE SATISFIED BY ANY MEMBERS RESPONSE TO YOUR ISSUE PLEASE USE THE STAR ICON AT THE BOTTOM LEFT OF THE POST UNDER THEIR NAME.

  10. #10
    Registered User
    Join Date
    12-20-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Format Canadian Postal Code on Entry

    Hi, if I want to apply that validation to a specific cell, say cell C14, how would I modify the VBCode?

  11. #11
    Registered User
    Join Date
    03-07-2017
    Location
    Edmonton, Canada
    MS-Off Ver
    Office 2007
    Posts
    1

    Re: Format Canadian Postal Code on Entry

    Quote Originally Posted by wils_venancio View Post
    Hi, if I want to apply that validation to a specific cell, say cell C14, how would I modify the VBCode?
    Did you ever figure this out?

  12. #12
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,047

    Re: Format Canadian Postal Code on Entry

    Chris, welcome to the forum

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  13. #13
    Registered User
    Join Date
    03-24-2019
    Location
    Granton Ont.
    MS-Off Ver
    365
    Posts
    1

    Re: Format Canadian Postal Code on Entry

    Hi Group,
    New here today Best to all
    Last edited by AAC_Forms; 03-24-2019 at 04:42 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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