Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 02-05-2007, 09:11 PM
Gos-C Gos-C is offline
Valued Forum Contributor
 
Join Date: 19 Sep 2004
Location: Canada
Posts: 255
Gos-C is becoming part of the community
Question Format Canadian Postal Code on Entry

Please Register to Remove these Ads

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 2007/Windows XP

"It is better to be prepared for an opportunity and not have one, than to have an opportunity and not be prepared."
Reply With Quote
  #2  
Old 02-05-2007, 11:20 PM
mudraker's Avatar
mudraker mudraker is offline
Forum Moderator
 
Join Date: 10 Nov 2003
Location: Melbourne, Australia
Posts: 4,235
mudraker has an addiction to Excel
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


Code:
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
Reply With Quote
  #3  
Old 02-06-2007, 09:10 AM
Gos-C Gos-C is offline
Valued Forum Contributor
 
Join Date: 19 Sep 2004
Location: Canada
Posts: 255
Gos-C is becoming part of the community
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
__________________
Using Excel 2007/Windows XP

"It is better to be prepared for an opportunity and not have one, than to have an opportunity and not be prepared."
Reply With Quote
  #4  
Old 02-06-2007, 05:30 PM
mudraker's Avatar
mudraker mudraker is offline
Forum Moderator
 
Join Date: 10 Nov 2003
Location: Melbourne, Australia
Posts: 4,235
mudraker has an addiction to Excel
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


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 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
Reply With Quote
  #5  
Old 02-06-2007, 06:05 PM
oldchippy's Avatar
oldchippy oldchippy is offline
"Eagle Eyed" Forum Moderator
 
Join Date: 14 Feb 2005
Location: Worcester, UK
MS Office Version:Excel 2007 (Work) and 2007 (Home)
Posts: 6,925
oldchippy is attaining expert status oldchippy is attaining expert status
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
-------------


If you are happy with the help you have received, please add to our reputation by clicking the blue scales icon in the blue bar of the post.

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

Forum Rules >>>Please don't forget to read these
Reply With Quote
  #6  
Old 02-07-2007, 10:34 AM
Gos-C Gos-C is offline
Valued Forum Contributor
 
Join Date: 19 Sep 2004
Location: Canada
Posts: 255
Gos-C is becoming part of the community
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 2007/Windows XP

"It is better to be prepared for an opportunity and not have one, than to have an opportunity and not be prepared."
Reply With Quote
  #7  
Old 02-07-2007, 04:53 PM
mudraker's Avatar
mudraker mudraker is offline
Forum Moderator
 
Join Date: 10 Nov 2003
Location: Melbourne, Australia
Posts: 4,235
mudraker has an addiction to Excel
I should have been a bit more thourgh on the testing before posting the code


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
Reply With Quote
  #8  
Old 02-07-2007, 09:45 PM
Gos-C Gos-C is offline
Valued Forum Contributor
 
Join Date: 19 Sep 2004
Location: Canada
Posts: 255
Gos-C is becoming part of the community
Got it! Mudraker, you're the best! Thanks a million.

Gos-C
__________________
Using Excel 2007/Windows XP

"It is better to be prepared for an opportunity and not have one, than to have an opportunity and not be prepared."
Reply With Quote
  #9  
Old 02-07-2007, 11:24 PM
mudraker's Avatar
mudraker mudraker is offline
Forum Moderator
 
Join Date: 10 Nov 2003
Location: Melbourne, Australia
Posts: 4,235
mudraker has an addiction to Excel
Glad to hear all is now working correctly & thanks for the feed back
Reply With Quote
  #10  
Old 02-08-2007, 02:06 AM
gajendra.gupta gajendra.gupta is offline
Registered User
 
Join Date: 19 Jan 2007
Location: Bangalore, India
Posts: 66
gajendra.gupta is becoming part of the community
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 With Quote
  #11  
Old 02-08-2007, 02:41 AM
gajendra.gupta gajendra.gupta is offline
Registered User
 
Join Date: 19 Jan 2007
Location: Bangalore, India
Posts: 66
gajendra.gupta is becoming part of the community
This is even more simpler..

Sub Postal_Verify()
If Range("D1") Like "[A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]" Then
Range("A1") = Left(Range("A1"), 3) & " " & Right(Range("A1"), 3)
Else
MsgBox "Invalid Postal Code."
End If
End Sub

Regards,
Gajendra Gupta
Reply With Quote
  #12  
Old 02-08-2007, 03:03 AM
mudraker's Avatar
mudraker mudraker is offline
Forum Moderator
 
Join Date: 10 Nov 2003
Location: Melbourne, Australia
Posts: 4,235
mudraker has an addiction to Excel
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


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 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
Reply With Quote
  #13  
Old 02-09-2007, 12:11 PM
Gos-C Gos-C is offline
Valued Forum Contributor
 
Join Date: 19 Sep 2004
Location: Canada
Posts: 255
Gos-C is becoming part of the community
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
__________________
Using Excel 2007/Windows XP

"It is better to be prepared for an opportunity and not have one, than to have an opportunity and not be prepared."

Last edited by Gos-C; 02-09-2007 at 12:14 PM.
Reply With Quote
  #14  
Old 02-09-2007, 05:18 PM
mudraker's Avatar
mudraker mudraker is offline
Forum Moderator
 
Join Date: 10 Nov 2003
Location: Melbourne, Australia
Posts: 4,235
mudraker has an addiction to Excel
Gos-C

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


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 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
Reply With Quote
  #15  
Old 02-09-2007, 06:43 PM
Gos-C Gos-C is offline
Valued Forum Contributor
 
Join Date: 19 Sep 2004
Location: Canada
Posts: 255
Gos-C is becoming part of the community
Thumbs up Job Well Done

Perfect!

Thanks again, Mudraker.

Gos-C
__________________
Using Excel 2007/Windows XP

"It is better to be prepared for an opportunity and not have one, than to have an opportunity and not be prepared."
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump