+ Reply to Thread
Results 1 to 27 of 27

Format Canadian Postal Code on Entry

Hybrid View

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

    Fantastic bit of code - I would never have thought of using Like

    I have modified my macro to incorporate your code.

    The new macro retains all the requirements as per Gos-C 1st posting and is much simpler than what I had coded


    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 Rng.Value Like "[A-Z][0-9][A-Z][ ][0-9][A-Z][0-9]" Then
                'do nothing
             Else
                MsgBox Rng.Address & " - invalid Postcode"
             End If
          End If
       Next Rng
       Application.EnableEvents = True
    End Sub

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

    This is certainly going to be a popular/much sought-after code. Good job! I think it would be better if the invalid postal code is cleared when the message is shown. Do you mind adding this last bit?

    Gajendra, thanks also for your great input.

    Regards,
    Gos-C
    Last edited by Gos-C; 02-09-2007 at 12:14 PM.
    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."

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

    Contents now cleared on invalid postcodes - New line shown in red


    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 Rng.Value Like "[A-Z][0-9][A-Z][ ][0-9][A-Z][0-9]" Then
                'do nothing
             Else
                MsgBox Rng.Address & " - invalid Postcode"
                Rng.ClearContents
             End If
          End If
       Next Rng
       Application.EnableEvents = True
    End Sub

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

    Thumbs up Job Well Done

    Perfect!

    Thanks again, Mudraker.

    Gos-C

  5. #5
    Registered User
    Join Date
    04-12-2012
    Location
    Toront, Canada
    MS-Off Ver
    Excel 2011
    Posts
    1

    Re: Format Canadian Postal Code on Entry

    Hello,

    I faces the same problem where i have a column of postal code and some of them are invalid. However, for some reason, I cannot run the macro. Nothing happened. I wonder why?

  6. #6
    Registered User
    Join Date
    01-20-2015
    Location
    Vancouver, Canada
    MS-Off Ver
    2007
    Posts
    1

    Re: Format Canadian Postal Code on Entry

    Just applied this to a project - still working in 2015! Thanks!

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

    Re: Format Canadian Postal Code on Entry

    Hi Iridian, I am happy you found it and were able to use it. Keep "excel-ling."

    Gos-C

  8. #8
    Registered User
    Join Date
    06-29-2015
    Location
    Winnipeg
    MS-Off Ver
    2010
    Posts
    1

    Re: Format Canadian Postal Code on Entry

    Hi Gos-C,

    I found the code and have inserted it (Excel 2010). The odd thing though is that the postal code is repeated five times. I hope the attached image is visible so you can see what I mean. Any ideas? Thanks for any help that you can provide!

    Chris

    PostalCode.JPG

+ 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