+ Reply to Thread
Results 1 to 4 of 4

Lookup city names in a string and then return the city code from a table

  1. #1
    Registered User
    Join Date
    10-18-2012
    Location
    Edmonton, Canada
    MS-Off Ver
    Excel 2010
    Posts
    22

    Lookup city names in a string and then return the city code from a table

    So, I have thousands of addresses - no consistent formatting ex:
    Column 1...………………………………………………………………………………. Column2
    123 Main Street, OLDS AB CANADA ……………………………………….Formula to return "OL"
    345-2 st ATHABASCA DRIVE, ATHABASCA AB CANADA...……..Formula to return "AT1"


    I have a table that provides the code ex:
    City...............City Code
    Olds......………...OL
    Athabasca...…...AT1

    I would like column2 on the first sheet to return the city code.

    Thank you all who take a look!
    Attached Files Attached Files
    Last edited by Babbabooie; 10-04-2021 at 03:02 PM. Reason: add table

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,152

    Re: Lookup city names in a string and then return the city code from a table

    See yellow banner at top of page on how to post a sample workbook.

    Is format always "City" "Province" CANADA? province 2 characters ?

    Try

    =VLOOKUP(TRIM(MID($A2,FIND(",",$A2)+2,FIND("CANADA",$A2)-FIND(",",$A2)-5)),$F$2:$G$100,2,FALSE)

    City/ City Code table in F2:G100
    Last edited by JohnTopley; 10-04-2021 at 02:22 PM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Registered User
    Join Date
    10-18-2012
    Location
    Edmonton, Canada
    MS-Off Ver
    Excel 2010
    Posts
    22

    Re: Lookup city names in a string and then return the city code from a table

    Thanks for replying. File sample attached

  4. #4
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Lookup city names in a string and then return the city code from a table

    Hi,

    The simplest would be:

    =LOOKUP(1,0/COUNTIF(A2,"*"&table!A$2:A$42&"*"),table!B$2:B$42)

    However, this is not certain to give correct results. For example, if A2 instead contained:

    GOLDSTONE ROAD W, LETHBRIDGE AB CANADA

    then the above formula would return "OL", not "LRPS", since "OLDS" is contained within "GOLDSTONE".

    To avoid this, it's generally better practice to search for the strings surrounded at either side by a single space:

    =LOOKUP(1,0/ISNUMBER(SEARCH(" "&table!A$2:A$42&" "," "&A2&" ")),table!B$2:B$42)

    though this would fail for cases such as:

    5100 65TH AVE ROCKY MOUNTAIN HOUSE, AB

    since there is a comma after "ROCKY MOUNTAIN HOUSE".

    So we can replace any commas with spaces:

    =LOOKUP(1,0/ISNUMBER(SEARCH(" "&table!A$2:A$42&" "," "&SUBSTITUTE(A2,","," ")&" ")),table!B$2:B$42)

    which will work providing other punctuation marks aren't also possible. If they are, you'll need to supply a definitive list of those punctuation which could occur within the addresses ('covering the bases' and adding clauses into the formula to account for all known punctuation would be overkill).

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Return a Metro City after matching a mailing city with a suburb
    By themole in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-18-2018, 10:16 AM
  2. Replies: 8
    Last Post: 10-16-2017, 03:08 AM
  3. Replies: 3
    Last Post: 02-07-2016, 03:58 AM
  4. Calculating Shipment Rates Base on Origin City and Destination City
    By Chnatko in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-09-2014, 11:11 AM
  5. IP address lookup - return city location
    By rtcwlomax in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-17-2013, 02:15 PM
  6. extract city using city list lookup
    By terrysoper1973 in forum Excel General
    Replies: 1
    Last Post: 09-07-2011, 01:43 PM
  7. Web lookup query (have: city & state | want: zip code)
    By avatarr in forum Excel General
    Replies: 0
    Last Post: 04-07-2010, 10:49 PM

Tags for this Thread

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