Extract Postal Codes from addresses

excel_1317

Board Regular
Joined
Jun 28, 2010
Messages
212
I have around 1000 addresses of about 60 different countries. These addresses are split into 5 columns. Need to extract postal codes in a new column.
Excel Workbook
ABCDE
1Attn Of/Address Line 1Address Line 2Address Line 3Address Line 4Address Line 5/Country Name
22040 Main Street14th FloorIrvine, CA 92614U.S.A.
3Ruppmannstrasse 27D-70565 StuttgartGermany
4Aeschenvorstadt 24P.O. Box 3184010 BaselSwitzerland
516, Akadimias str.10671 AthensGreece
61725 Duke St., Suite 240Alexandria, VA 22314U.S.A.
7Bavariaring 2080336 MunchenGermany
867 Hayarkon str.Bnei Brak 51206Israel
91185 Avenue of the AmericasNew York, NY 10036U.S.A.
10401 Congress Ave., Suite 3200Austin, Texas 78701U.S.A.
11109, Sector 44, Gurgaon 122 00National Capital RegionIndia
122-3 Fuji BldgMarunouchi 3-ChomeChiyoda-KuTokyo 100-0005Japan
1313 Haraduf Str.Omer 84965Israel
1454 Derech Bet Lechem str.Jerusalem 93504Israel
15Lackenbach Siegel BuildingOne Chase RoadScarsdale, New York 10583U.S.A.
16Mail Box No. 15Thai Thinh Post Office35 Thai Thinh StreetDong Da District, HanoiVietnam
171040 Avenue of the AmericasNew York, NY 10018-3738U.S.A.
18Kibutz LahavMobile Post Office 85335Israel
1914 David Elazar str. 6Beer Sheva 84509Israel
2028 Hazait Str.Meitar 85025Israel
21B-6/10, Safdarjung Enclave,New Delhi - 110 029India
22POSTFACH 111206900 HEIDELBERG 1Germany
23Riverfront Office ParkOne Main StreetEleventh FloorCambridge, MA 02142U.S.A.
241500 Norwest Financial Center7900 Xerxes Avenue SouthBloomington, Minnesota55431-1194U.S.A.
2562 Rue de Bonnel69448 Lyon Cedex 03France
Sheet3
Excel 2010


Postal code can be in Column A, B C or D. Column E has the country. Please help me extracting the postal codes in a new column F.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Could you describe in words how to determine the postal code in a row of data?

Not being an expert in international postal codes, could you provide the expected results for that set of sample data?
I'm particularly interested in the results for rows 11, 16 and 25.
 
Upvote 0
Could you describe in words how to determine the postal code in a row of data?

Not being an expert in international postal codes, could you provide the expected results for that set of sample data?
I'm particularly interested in the results for rows 11, 16 and 25.


Most Asian countries donot have postal codes and that is the case with row 16 where country is Vietnam. For row 11, postal code is in Column A i.e. 122 00. For row 25, Postal code is in column B i.e. 69448.

Here is the sample data with results.
Excel Workbook
ABCDEF
1Attn Of/Address Line 1Address Line 2Address Line 3Address Line 4Address Line 5/Country NameResult
22040 Main Street14th FloorIrvine, CA 92614U.S.A.92614
3Ruppmannstrasse 27D-70565 StuttgartGermanyD-70565
4Aeschenvorstadt 24P.O. Box 3184010 BaselSwitzerland4010
516, Akadimias str.10671 AthensGreece10671
61725 Duke St., Suite 240Alexandria, VA 22314U.S.A.22314
7Bavariaring 2080336 MunchenGermany80336
867 Hayarkon str.Bnei Brak 51206Israel51206
91185 Avenue of the AmericasNew York, NY 10036U.S.A.10036
10401 Congress Ave., Suite 3200Austin, Texas 78701U.S.A.78701
11109, Sector 44, Gurgaon 122 00National Capital RegionIndia122 00
122-3 Fuji BldgMarunouchi 3-ChomeChiyoda-KuTokyo 100-0005Japan100-0005
1313 Haraduf Str.Omer 84965Israel84965
1454 Derech Bet Lechem str.Jerusalem 93504Israel93504
15Lackenbach Siegel BuildingOne Chase RoadScarsdale, New York 10583U.S.A.10583
16Mail Box No. 15Thai Thinh Post Office35 Thai Thinh StreetDong Da District, HanoiVietnamNo Postal Code
171040 Avenue of the AmericasNew York, NY 10018-3738U.S.A.10018-3738
18Kibutz LahavMobile Post Office 85335Israel85335
1914 David Elazar str. 6Beer Sheva 84509Israel84509
2028 Hazait Str.Meitar 85025Israel85025
21B-6/10, Safdarjung Enclave,New Delhi - 110 029India110029
22POSTFACH 111206900 HEIDELBERG 1Germany11120
23Riverfront Office ParkOne Main StreetEleventh FloorCambridge, MA 02142U.S.A.02142
241500 Norwest Financial Center7900 Xerxes Avenue SouthBloomington, Minnesota55431-1194U.S.A.55431
2562 Rue de Bonnel69448 Lyon Cedex 03France69448
Sheet3
Excel 2010
 
Upvote 0
Thanks for providing the expected results and some further explanation. Unfortunately you didn't provide an answer to my first question.

Any solution in Excel will require some logical process(es) to extract the information.

I had thought that perhaps the logic might be to find the last numeric (possibly including dash or space) value from the row. However, that would not be the case for rows 3 (code includes a letter), 16 (neither of the numeric values is a postal code), 22 (neither last nor second last numeric is postal code) and 25 (it is the second last numeric that is the postal code. (I'm assuming your expected result for row 24 is a typo and the result should be 55431-1194?).

So unless some sort of rule(s) can be established for what is a postal code and what is not, I can't see how an Excel solution can be developed.

Given that the postal codes are scattered throughout the columns, if some method can be established, it would most likely have to be implemented by a macro so that would also have to be acceptable to you.
 
Upvote 0
Unfortunately, I'm not going to come with a finished suggestion, but more some "worries".

First of all, are you 110% certain that the names of the countries in E will always be those values (by that I mean U.S.A. and not USA). Once you start getting variations on the names, it gets a a whole load more complicated.
Secondly, you didn't specify whether this should be solved by "excel programming" or whether you could write a VBA macro to handle it.

If a macro is acceptable, then the logic shouldn't be that difficult, but again, this assumes that the addresses you have don't contain any surprises (and I'll bet they will do at some time).

In fact, on reflection, I would probably go with the macro solution, since that will be easier to change for any "unexpected" variations in the data.

Reviewing your results, I see discrepancies in the results for lines 11 & 21. Line 11 uses column A to extract the postal code, line 21 uses column B and yet they're both Indian addresses. How on earth do you intend to solve this ? You'll end up with code (for India) that says, take the last 2 words from column A, are they numeric when catenated ?
Okay they weren't, then use column B. I can already see loads of exceptions to specific rules
 
Last edited:
Upvote 0
Thanks for providing the expected results and some further explanation. Unfortunately you didn't provide an answer to my first question.

Any solution in Excel will require some logical process(es) to extract the information.

I had thought that perhaps the logic might be to find the last numeric (possibly including dash or space) value from the row. However, that would not be the case for rows 3 (code includes a letter), 16 (neither of the numeric values is a postal code), 22 (neither last nor second last numeric is postal code) and 25 (it is the second last numeric that is the postal code. (I'm assuming your expected result for row 24 is a typo and the result should be 55431-1194?).

So unless some sort of rule(s) can be established for what is a postal code and what is not, I can't see how an Excel solution can be developed.

Given that the postal codes are scattered throughout the columns, if some method can be established, it would most likely have to be implemented by a macro so that would also have to be acceptable to you.


To give a logic, we can concatenate the whole address leaving country column. From there a macro can be written on the basis of country name. For eg. When the country is U.S.A the the macro will look for only 5 digit numbers....

I need for 5 major countries.. I thought of a logic that can possibly be used..
Excel Workbook
ABC
1CountrySample Postal CodeLogic
2U.S.A.926145 digit numeric
3Israel849655 digit numeric
4GermanyD-705655 digit alphanumeric starting with D-
5United KingdomHX7 7AF6 characters alphabet with space after first 3 characters
6France312505 digit numeric
7Italy201315 digit numeric
Sheet5
Excel 2010
 
Upvote 0
To give a logic, we can concatenate the whole address leaving country column. From there a macro can be written on the basis of country name. For eg. When the country is U.S.A the the macro will look for only 5 digit numbers....

I need for 5 major countries.. I thought of a logic that can possibly be used..
Excel Workbook
ABC
1CountrySample Postal CodeLogic
2U.S.A.926145 digit numeric
3Israel849655 digit numeric
4GermanyD-705655 digit alphanumeric starting with D-
5United KingdomHX7 7AF6 characters alphabet with space after first 3 characters
6France312505 digit numeric
7Italy201315 digit numeric
Sheet5
Excel 2010
OK, that's moving in the right direction at least but some further questions/comments.

1. The USA rule does not match with your expected result in row 17. Is the correct result for that row actually 10018?

2. Your logic for Germany does not appear to have any relevance to your expected results or the data in rows 7 or 22. Is there more we need to know there?

3. Haven't seen any sample data/results for UK or Italy so not sure about those.

In any case, see if this is any use.
Code:
Sub Extract_PC()
  Dim a
  Dim i As Long, j As Long, k As Long
  Dim rws As Long, numCountries As Long, CountryNum As Long
  Dim aCountries, aPCPatterns
  Dim RX As Object
  Dim s As String, PC As String
  
  Const Countries As String = "U.S.A.," & _
                              "Israel," & _
                              "Germany," & _
                              "United Kingdom," & _
                              "France," & _
                              "Italy"
                              
  Const PCPatterns As String = "\b\d{5}\b," & _
                              "\b\d{5}\b," & _
                              "\bD-\d{5}\b," & _
                              "\b[0-9A-Z]{3} [0-9A-Z]{3}\b," & _
                              "\b\d{5}\b," & _
                              "\b\d{5}\b,"
  
  aCountries = Split(Countries, ",")
  aPCPatterns = Split(PCPatterns, ",")
  numCountries = UBound(aCountries) + 1
  Set RX = CreateObject("VBScript.RegExp")
  With Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 6)
    a = .Value
    rws = UBound(a, 1)
    For i = 1 To rws
      s = a(i, 5)
      CountryNum = 0
      For k = 1 To numCountries
        If s = aCountries(k - 1) Then
          CountryNum = k
        End If
      Next k
      If CountryNum > 0 Then
        RX.Pattern = aPCPatterns(CountryNum - 1)
        j = 4
        PC = ""
        Do
          s = a(i, j)
          If RX.test(s) Then
            PC = RX.Execute(s)(0)
          Else
            j = j - 1
          End If
        Loop While PC = "" And j > 0
        If Len(PC) > 0 Then
          a(i, 6) = PC
        End If
      End If
    Next i
    .Columns(6).NumberFormat = "@"
    .Value = a
  End With
End Sub

Results as follows (I've added one UK line):

Excel Workbook
ABCDEF
1Attn Of/Address Line 1Address Line 2Address Line 3Address Line 4Address Line 5/Country NameResult
22040 Main Street14th FloorIrvine, CA 92614U.S.A.92614
3Ruppmannstrasse 27D-70565 StuttgartGermanyD-70565
4Aeschenvorstadt 24P.O. Box 3184010 BaselSwitzerland
516, Akadimias str.10671 AthensGreece
61725 Duke St., Suite 240Alexandria, VA 22314U.S.A.22314
7Bavariaring 2080336 MunchenGermany
867 Hayarkon str.Bnei Brak 51206Israel51206
91185 Avenue of the AmericasNew York, NY 10036U.S.A.10036
10401 Congress Ave., Suite 3200Austin, Texas 78701U.S.A.78701
11109, Sector 44, Gurgaon 122 00National Capital RegionIndia
122-3 Fuji BldgMarunouchi 3-ChomeChiyoda-KuTokyo 100-0005Japan
1313 Haraduf Str.Omer 84965Israel84965
1454 Derech Bet Lechem str.Jerusalem 93504Israel93504
15Lackenbach Siegel BuildingOne Chase RoadScarsdale, New York 10583U.S.A.10583
16Mail Box No. 15Thai Thinh Post Office35 Thai Thinh StreetDong Da District, HanoiVietnam
171040 Avenue of the AmericasNew York, NY 10018-3738U.S.A.10018
18Kibutz LahavMobile Post Office 85335Israel85335
1914 David Elazar str. 6Beer Sheva 84509Israel84509
2028 Hazait Str.Meitar 85025Israel85025
21B-6/10, Safdarjung Enclave,New Delhi - 110 029India
22POSTFACH 111206900 HEIDELBERG 1Germany
23Riverfront Office ParkOne Main StreetEleventh FloorCambridge, MA 02142U.S.A.02142
241500 Norwest Financial Center7900 Xerxes Avenue SouthBloomington, Minnesota55431-1194U.S.A.55431
2562 Rue de Bonnel69448 Lyon Cedex 03France69448
26123 Smith laneSomewhere HX7 7AFUnited KingdomHX7 7AF
27
Postal Codes
 
Upvote 0
Thanks for this. I have copied the same in excel module. But how to use this?


OK, that's moving in the right direction at least but some further questions/comments.

1. The USA rule does not match with your expected result in row 17. Is the correct result for that row actually 10018?

2. Your logic for Germany does not appear to have any relevance to your expected results or the data in rows 7 or 22. Is there more we need to know there?

3. Haven't seen any sample data/results for UK or Italy so not sure about those.

In any case, see if this is any use.
Code:
Sub Extract_PC()
  Dim a
  Dim i As Long, j As Long, k As Long
  Dim rws As Long, numCountries As Long, CountryNum As Long
  Dim aCountries, aPCPatterns
  Dim RX As Object
  Dim s As String, PC As String
  
  Const Countries As String = "U.S.A.," & _
                              "Israel," & _
                              "Germany," & _
                              "United Kingdom," & _
                              "France," & _
                              "Italy"
                              
  Const PCPatterns As String = "\b\d{5}\b," & _
                              "\b\d{5}\b," & _
                              "\bD-\d{5}\b," & _
                              "\b[0-9A-Z]{3} [0-9A-Z]{3}\b," & _
                              "\b\d{5}\b," & _
                              "\b\d{5}\b,"
  
  aCountries = Split(Countries, ",")
  aPCPatterns = Split(PCPatterns, ",")
  numCountries = UBound(aCountries) + 1
  Set RX = CreateObject("VBScript.RegExp")
  With Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 6)
    a = .Value
    rws = UBound(a, 1)
    For i = 1 To rws
      s = a(i, 5)
      CountryNum = 0
      For k = 1 To numCountries
        If s = aCountries(k - 1) Then
          CountryNum = k
        End If
      Next k
      If CountryNum > 0 Then
        RX.Pattern = aPCPatterns(CountryNum - 1)
        j = 4
        PC = ""
        Do
          s = a(i, j)
          If RX.test(s) Then
            PC = RX.Execute(s)(0)
          Else
            j = j - 1
          End If
        Loop While PC = "" And j > 0
        If Len(PC) > 0 Then
          a(i, 6) = PC
        End If
      End If
    Next i
    .Columns(6).NumberFormat = "@"
    .Value = a
  End With
End Sub

Results as follows (I've added one UK line):

Postal Codes

*ABCDEF
1Attn Of/Address Line 1Address Line 2Address Line 3Address Line 4Address Line 5/Country NameResult
22040 Main Street14th FloorIrvine, CA 92614*U.S.A.92614
3Ruppmannstrasse 27D-70565 Stuttgart**GermanyD-70565
4Aeschenvorstadt 24P.O. Box 3184010 Basel*Switzerland*
516, Akadimias str.10671 Athens**Greece*
61725 Duke St., Suite 240Alexandria, VA 22314**U.S.A.22314
7Bavariaring 2080336 Munchen**Germany*
867 Hayarkon str.Bnei Brak 51206**Israel51206
91185 Avenue of the AmericasNew York, NY 10036**U.S.A.10036
10401 Congress Ave., Suite 3200Austin, Texas 78701**U.S.A.78701
11109, Sector 44, Gurgaon 122 00National Capital Region**India*
122-3 Fuji BldgMarunouchi 3-ChomeChiyoda-KuTokyo 100-0005Japan*
1313 Haraduf Str.Omer 84965**Israel84965
1454 Derech Bet Lechem str.Jerusalem 93504**Israel93504
15Lackenbach Siegel BuildingOne Chase RoadScarsdale, New York 10583*U.S.A.10583
16Mail Box No. 15Thai Thinh Post Office35 Thai Thinh StreetDong Da District, HanoiVietnam*
171040 Avenue of the AmericasNew York, NY 10018-3738**U.S.A.10018
18Kibutz LahavMobile Post Office 85335**Israel85335
1914 David Elazar str. 6Beer Sheva 84509**Israel84509
2028 Hazait Str.Meitar 85025**Israel85025
21B-6/10, Safdarjung Enclave,New Delhi - 110 029**India*
22POSTFACH 111206900 HEIDELBERG 1**Germany*
23Riverfront Office ParkOne Main StreetEleventh FloorCambridge, MA 02142U.S.A.02142
241500 Norwest Financial Center7900 Xerxes Avenue SouthBloomington, Minnesota55431-1194U.S.A.55431
2562 Rue de Bonnel69448 Lyon Cedex 03**France69448
26123 Smith laneSomewhere HX7 7AF**United KingdomHX7 7AF
27******

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:208px;"><col style="width:181px;"><col style="width:181px;"><col style="width:154px;"><col style="width:131px;"><col style="width:69px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
 
Upvote 0
Thanks for this. I have copied the same in excel module. But how to use this?
With your data sheet active press Alt+F8, select the Extract_PC macro and click Run.

Make sure you test in a copy of your workbook.
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top