+ 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
    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

  2. #2
    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

  3. #3
    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
    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."

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    I should have been a bit more thourgh on the testing before posting the code


    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 iChar
                   Case 1, 3, 4, 6
                      MsgBox Rng.Address & " - " & Mid(Rng.Value, iChar, 1) _
                      & " is invalid for Postcode"
                      'rng.ClearContents
                      Exit For
                   End Select
                Case " "
                   Select Case iChar
                   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 Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408
    Got it! Mudraker, you're the best! Thanks a million.

    Gos-C

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Glad to hear all is now working correctly & thanks for the feed back

  7. #7
    Registered User
    Join Date
    01-19-2007
    Location
    Bangalore, India
    Posts
    66
    I beleive this might also helpful if you are entering Postcode in Range 'A1'.

    Sub Postal_Verify()
    If Len(Range("A1").Text) = 6 And _
    IsAlpha(Mid(Range("A1").Text, 1, 1)) = True And _
    IsAlpha(Mid(Range("A1").Text, 3, 1)) = True And _
    IsAlpha(Mid(Range("A1").Text, 5, 1)) = True And _
    IsNumeric(Mid(Range("A1").Text, 2, 1)) = True And _
    IsNumeric(Mid(Range("A1").Text, 4, 1)) = True And _
    IsNumeric(Mid(Range("A1").Text, 6, 1)) = True Then
    Range("A1") = Left(Range("A1"), 3) & " " & Right(Range("A1"), 3)
    Else
    MsgBox "Invalid Postal Code."
    End If
    End Sub
    Function IsAlpha(chr As String) As Boolean
    If Asc(chr) >= 97 And Asc(chr) <= 122 Or Asc(chr) >= 65 And Asc(chr) <= 90 Then
    IsAlpha = True
    Else
    IsAlpha = False
    End If
    End Function

    Regards,
    Gajendra Gupta

+ 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