Extract City & State from string

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have data in one cell and need to extract in column B the city & state

Any suggestions?

Before
2330 Tittabawassee Rd Saginaw MI 48604
22600 Hall Rd #204 Clinton Twp MI 48036
3254 Stadium Dr Suite B Kalamazoo MI 49008
3446 S Linden Rd Flint MI 48507
5122 E Lincoln Hwy Merrillville IN 46410
555 S. Reynolds Rd Toledo OH 43615
1037 North Michell St Cadillac MI 49601
47178 Hayes Rd. Macomb MI 48044

After
Saginaw MI
Clinton Twp MI
Kalamazoo MI
Flint MI
Merrillville IN
Toledo OH
Cadillac MI
Macomb MI
 
On further inspection, there are three that didn't convert right, but in the end at least one of them may not be corrected through the code.

Before
4641 Westport Drive Air Force Mechanicsburg PA 17055
1500 W. Chestnut Street (Washington Crown Center Mall) Washington PA 15301
4933 Whipple Ave NW Canton OH 44718

After
Air Force Mechanicsburg PA
Mall) Washington PA
NW Canton OH

What it should be
Mechanicsburg PA
Washington PA
Canton OH

I know these are different than the Michigan I originally posted, but I'm going to be running this for many different states.
I don't understand what the "Air Force" is doing in that first one, but any extraneous text, or misspelled street types (Raod instead of Road, for example) for that matter, will generate a corrupt address... I cannot think of anything to do to combat this short of having a dictionary of every city name in the US (kind of impractical I would think). The third one is the one I warned about in the comment I placed at the beginning of my code... again, nothing can be done about that either. However, I think I can partially handle the problem in that second address. Again, it's not, and cannot be made, foolproof (too many things could be placed in parentheses, but the following code will catch those that can be caught...
Code:
'  Post-directionals on street types (123 Somewhere Rd North, or 456 Something Blvd E.,
'  for example) will be interpreted incorrectly... and nothing can be done about it
'  because it could be that the "North" or "E." belongs to the city name instead; so
'  the resulting list will have to be inspected for those possibilities.
Sub CityState()
  Dim R As Long, X As Long, Z As Long, Data As Variant
  Dim Parts() As String, Result() As String
  Const Abbr1 = " ALLEE ALLEY ALLY ALY ANEX ANNEX ANNX ANX ARC ARCADE AV AVE AVEN AVENU" & _
                " AVENUE AVN AVNUE BAYOO BAYOU BCH BEACH BEND BND BLF BLUF BLUFF BLUFFS" & _
                " BOT BTM BOTTM BOTTOM BLVD BOUL BOULEVARD BOULV BR BRNCH BRANCH BRDGE" & _
                " BRG BRIDGE BRK BROOK BROOKS BURG BURGS BYP BYPA BYPAS BYPASS BYPS" & _
                " CAMP CP CMP CANYN CANYON CNYN CAPE CPE CAUSEWAY CAUSWA CSWY CEN CENT" & _
                " CENTER CENTR CENTRE CNTER CNTR CTR CENTERS CIR CIRC CIRCL CIRCLE CRCL" & _
                " CRCLE CIRCLES CLF CLIFF CLFS CLIFFS CLB CLUB COMMON COMMONS COR" & _
                " CORNER CORNERS CORS COURSE CRSE COURT CT COURTS CTS COVE CV COVES" & _
                " CREEK CRK CRESCENT CRES CRSENT CRSNT CREST CROSSING CRSSNG XING" & _
                " CROSSROAD CROSSROADS CURVE DALE DL DAM DM DIV DIVIDE DV DVD DR DRIV" & _
                " DRIVE DRV DRIVES EST ESTATE ESTATES ESTS EXP EXPR EXPRESS EXPRESSWAY" & _
                " EXPW EXPY EXT EXTENSION EXTN EXTNSN EXTS FALL FALLS FLS FERRY FRRY" & _
                " FRY FIELD FLD FIELDS FLDS FLAT FLT FLATS FLTS FORD FRD FORDS FOREST" & _
                " FORESTS FRST FORG FORGE FRG FORGE FORK FRK FORKS FRKS FORT FRT FT" & _
                " FREEWAY FREEWY FRWAY FRWY FWY GARDEN GARDN GRDEN GRDN GARDENS GDNS" & _
                " GRDNS GATEWAY GATEWY GATWAY GTWAY GTWY GLEN GLN GLENS GREEN GRN" & _
                " GREENS GROV GROVE GRV GROVES HARB HARBOR HARBR HBR HRBOR HARBORS" & _
                " HAVEN HVN HT HTS HIGHWAY HIGHWY HIWAY HIWY HWAY HWY HILL HL HILLS" & _
                " HLS HLLW HOLLOW HOLLOWS HOLW HOLWS INLT IS ISLAND ISLND ISLANDS" & _
                " ISLNDS ISS ISLE ISLES JCT JCTION JCTN JUNCTION JUNCTN JUNCTON" & _
                " JCTNS JCTS JUNCTIONS KEY KY KEYS KYS KNL KNOL KNOLL KNLS KNOLLS LK" & _
                " LAKE LKS LAKES LAND LANDING LNDG LNDNG LANE LN LGT LIGHT LIGHTS LF"
  Const Abbr = Abbr1 & " LOAF LCK LOCK LCKS LOCKS LDG LDGE LODG LODGE LOOP LOOPS MALL" & _
               " MNR MANOR MANORS MNRS MEADOW MDW MDWS MEADOWS MEDOWS MEWS MILL MILLS" & _
               " MISSN MSSN MOTORWAY MNT MT MOUNT MNTAIN MNTN MOUNTAIN MOUNTIN MTIN" & _
               " MTN MNTNS MOUNTAINS NCK NECK ORCH ORCHARD ORCHRD OVAL OVL OVERPASS" & _
               " PARK PRK PARKS PARKWAY PARKWY PKWAY PKWY PKY PARKWAYS PKWYS PASS" & _
               " PASSAGE PATH PATHS PIKE PIKES PINE PINES PNES PL PLAIN PLN PLAINS" & _
               " PLNS PLAZA PLZ PLZA POINT PT POINTS PTS PORT PRT PORTS PRTS PR" & _
               " PRAIRIE PRR RAD RADIAL RADIEL RADL RAMP RANCH RANCHES RNCH RNCHS" & _
               " RAPID RPD RAPIDS RPDS REST RST RDG RDGE RIDGE RDGS RIDGES RIV RIVER" & _
               " RVR RIVR RD ROAD ROADS RDS ROUTE ROW RUE RUN SHL SHOAL SHLS SHOALS" & _
               " SHOAR SHORE SHR SHOARS SHORES SHRS SKYWAY SPG SPNG SPRING SPRNG" & _
               " SPGS SPNGS SPRINGS SPRNGS SPUR SPURS SQ SQR SQRE SQU SQUARE SQRS" & _
               " SQUARES STA STATION STATN STN STRA STRAV STRAVEN STRAVENUE STRAVN" & _
               " STRVN STRVNUE STREAM STREME STRM STREET STRT ST STR STREETS SMT SUMIT" & _
               " SUMITT SUMMIT TER TERR TERRACE THROUGHWAY TRACE TRACES TRCE TRACK" & _
               " TRACKS TRAK TRK TRKS TRAFFICWAY TRAIL TRAILS TRL TRLS TRAILER TRLR" & _
               " TRLRS TUNEL TUNL TUNLS TUNNEL TUNNELS TUNNL TRNPK TURNPIKE TURNPK" & _
               " UNDERPASS UN UNION UNIONS VALLEY VALLY VLLY VLY VALLEYS VLYS VDCT" & _
               " VIA VIADCT VIADUCT VIEW VW VIEWS VWS VILL VILLAG VILLAGE VILLG" & _
               " VILLIAGE VLG VILLAGES VLGS VILLE VL VIS VIST VISTA VST VSTA WALK" & _
               " WALKS WALL WY WAY WAYS WELL WELLS WLS "
  Const Unit = " SUITE STE APARTMENT APTMT APT FLOOR FLR FL BUILDING " & _
               " BUILD BLDG BLD BLG OFFICE OFF OFC ROOM RM UNIT UNT UN "
  Data = Range("A1", Cells(Rows.Count, "A").End(xlUp))
  ReDim Result(1 To UBound(Data), 1 To 1)
  For R = 1 To UBound(Data)
    Parts = Split(Data(R, 1))
    For X = UBound(Parts) - 3 To 1 Step -1
      If Parts(X) Like "*[0-9#]*" Or InStr(Abbr & Unit, " " & _
          UCase(Replace(Replace(Parts(X), ".", ""), ")", "")) & " ") > 0 Then
        For Z = X + 1 - (InStr(Unit, " " & UCase(Replace(Replace(Parts(X), _
                       ".", ""), ")", "")) & " ") > 0) To UBound(Parts) - 1
          Result(R, 1) = Result(R, 1) & " " & Parts(Z)
        Next
        Result(R, 1) = Trim(Result(R, 1))
        Exit For
      End If
    Next
    Range("B1").Resize(UBound(Result)) = Result
  Next
End Sub
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Yes, thanks again Rick.

I am perfectly fine with a 95% fix because that 5% is nothing
 
Upvote 0
To get the list of US cities and towns:

Go to

http://www.census.gov/popest/data/cities/totals/2011/files/SUB-EST2011-IP.csv

and copy/paste the list in column A of an excel sheet, convert Text to columns choosing comma as a delimiter, then delete all the columns except column D. Then enter into E2 and copy down:

=SUBSTITUTE(D2," "&TRIM(RIGHT(SUBSTITUTE(D2," ",REPT(" ",20)),20)),"")

to get the list of ’places’.

Some place is named like this: ’Juneau city and borough’, these structures can be reduced to „Juneau’ etc. with another helper column easily.
 
Upvote 0
To get the list of US cities and towns:

Go to

http://www.census.gov/popest/data/cities/totals/2011/files/SUB-EST2011-IP.csv

and copy/paste the list in column A of an excel sheet, convert Text to columns choosing comma as a delimiter, then delete all the columns except column D. Then enter into E2 and copy down:

=SUBSTITUTE(D2," "&TRIM(RIGHT(SUBSTITUTE(D2," ",REPT(" ",20)),20)),"")

to get the list of ’places’.

Some place is named like this: ’Juneau city and borough’, these structures can be reduced to „Juneau’ etc. with another helper column easily.
After that, the list would need to be sorted by length, from longest to shortest, otherwise things like the Oaks (in Oklahoma) would be found when looking to verify Thousand Oaks (in California). Then, I am guessing, the list would have to be iterated one-at-a-time for each row being checked... I'm thinking that may be a lengthy process if the list being checked is very large.
 
Upvote 0
After that, the list would need to be sorted by length, from longest to shortest, otherwise things like the Oaks (in Oklahoma) would be found when looking to verify Thousand Oaks (in California). Then, I am guessing, the list would have to be iterated one-at-a-time for each row being checked... I'm thinking that may be a lengthy process if the list being checked is very large.

One easy way of producing US ’place’ names has been shown in my post, without going into every detail of using it in the present case.

No doubt, the method based on the USPS Abbreviations List seems simpler and much more reliable than the one based on the „place” name itself would be.
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,098
Members
449,205
Latest member
ralemanygarcia

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