ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   international postal codes (https://www.excelbanter.com/excel-worksheet-functions/165493-international-postal-codes.html)

JJ Johnson

international postal codes
 
Every month I need to validate internation postal codes for acceptable
formats in an Excel 2003 spreadsheet. Some countries have numerous
acceptable formats (up to about 20 or so for some) using letters and numbers
and sometimes the initials of the country. For example:

l = letter
n = number

Finland
nnnnn
FI nnnnn
FI-nnnnn
FInnnnn
FIN nnnnn
FIN-nnnnn
FINnnnnn

Canada
lnl nln
lnl-nln
lnlnln

Is there a function in Excel 2003 that can do this kind of validation?
--
JJ Johnson

Bernard Liengme

international postal codes
 
Actually the Canada Post like to see LNL NLN with the space
best wishes
--
Bernard V Liengme
Microsoft Excel MVP
www.stfx.ca/people/bliengme
remove caps from email

"JJ Johnson" wrote in message
...
Every month I need to validate internation postal codes for acceptable
formats in an Excel 2003 spreadsheet. Some countries have numerous
acceptable formats (up to about 20 or so for some) using letters and
numbers
and sometimes the initials of the country. For example:

l = letter
n = number

Finland
nnnnn
FI nnnnn
FI-nnnnn
FInnnnn
FIN nnnnn
FIN-nnnnn
FINnnnnn

Canada
lnl nln
lnl-nln
lnlnln

Is there a function in Excel 2003 that can do this kind of validation?
--
JJ Johnson




Ross[_3_]

international postal codes
 
Hi JJ,

Firstly you would need to collate the various international postal code
exmaples.

Once you have these you can continue to write VBA code and use IF to verify
the country and postal code format. Add a comment in another column.

For example;
Lets say for this example you have "Canada" entered in cell "A1" and the
exmaple postal code of "A1A 2B2" in cell "B1" in your spreadsheet.

Open the Visual Basic for Applications Editor (Alt-F11) and enter/copy the
following code;

Sub checkPostalCodes()

'Declare Variables
Dim CountryRegion As String
Dim PostalCode As String

'Assign Spreadsheet reference
CountryRegion = Range("A1")
PostalCode = Range("B1")

'Use if function to check postal code is correct
If CountryRegion = "Canada" And PostalCode Like "[A-Z][0-9][A-Z]
[0-9][A-Z][0-9]" Then
'if true
Range("C1") = "OK"
Else
'if false
Range("C1") = "Error"
End If

'Finish
End Sub

All you would have to do know is use a loop to process through all your
records/rows of data. I hope this helps, and if you want more information
on Postal Codes and Macros then visit the MSDN web address below;

http://msdn2.microsoft.com/en-us/library/bb177324.aspx

Cheers

Ross


"JJ Johnson" wrote in message
...
Every month I need to validate internation postal codes for acceptable
formats in an Excel 2003 spreadsheet. Some countries have numerous
acceptable formats (up to about 20 or so for some) using letters and
numbers
and sometimes the initials of the country. For example:

l = letter
n = number

Finland
nnnnn
FI nnnnn
FI-nnnnn
FInnnnn
FIN nnnnn
FIN-nnnnn
FINnnnnn

Canada
lnl nln
lnl-nln
lnlnln

Is there a function in Excel 2003 that can do this kind of validation?
--
JJ Johnson



Ross[_3_]

international postal codes
 
Hi JJ,

Sorry I forgot to add the "variations" to that code. See below, it will
also test the different variations for Canada;
lnl nln
lnl-nln
lnlnln

Sub checkPostalCodes()

'Declare Variables
Dim CountryRegion As String
Dim PostalCode As String

'Assign Spreadsheet reference
CountryRegion = Range("A1")
PostalCode = Range("B1")

'Use if function to check postal code is correct
If CountryRegion = "Canada" And PostalCode Like "[A-Z][0-9][A-Z]
[0-9][A-Z][0-9]" Then
'various if true
Range("C1") = "OK"
ElseIf CountryRegion = "Canada" And PostalCode Like
"[A-Z][0-9][A-Z]-[0-9][A-Z][0-9]" Then
Range("C1") = "OK"
ElseIf CountryRegion = "Canada" And PostalCode Like
"[A-Z][0-9][A-Z][0-9][A-Z][0-9]" Then
Range("C1") = "OK"
Else
'if false
Range("C1") = "Error"
End If

'Finish
End Sub

Hope this helps

Cheers

Ross



"JJ Johnson" wrote in message
...
Every month I need to validate internation postal codes for acceptable
formats in an Excel 2003 spreadsheet. Some countries have numerous
acceptable formats (up to about 20 or so for some) using letters and
numbers
and sometimes the initials of the country. For example:

l = letter
n = number

Finland
nnnnn
FI nnnnn
FI-nnnnn
FInnnnn
FIN nnnnn
FIN-nnnnn
FINnnnnn

Canada
lnl nln
lnl-nln
lnlnln

Is there a function in Excel 2003 that can do this kind of validation?
--
JJ Johnson




All times are GMT +1. The time now is 05:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com