Extract City & State from string

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,362
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
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Sub ParseAddr()
Dim vAddr, vST, vZip, vWord


Range("A2").Select
While ActiveCell.Value <> ""
   vWord = ActiveCell.Value
   i = InStrRev(vWord, " ")
   vZip = Right(vWord, i)
   vST = Mid(vWord, i - 2, 2)
   vAddr = Left(vWord, i - 3)
   
   ActiveCell.Offset(0, 1).Value = vAddr
   ActiveCell.Offset(0, 2).Value = vST
   ActiveCell.Offset(0, 3).Value = vZip
   
   ActiveCell.Offset(1, 0).Select   'next row
Wend
End Sub
 
Upvote 0
Practically impossible.
Look for a pattern.

Last word in cell is your 5 zip code
Previous word is your 2 letter state abbreviation
Then you've got a problem...

Saginaw MI 48604 - Previous word is the town Saginaw.
but your next one you need to extract the previous TWO words, Clinton AND Twp.
So there's no consistency.

Given one of your examples do you extract one or two words for the town?
Unless you have a table of every town in the states and perform a lookup I'd say it can't be done.

Is this really how the data has been given to you,
a string of words and numbers separated by spaces with no specific delimiter so you don't know where the town starts?

If the data contained commas that would be a start, you could use them as delimiters
 
Upvote 0
Code:
Sub ParseAddr()
Dim vAddr, vST, vZip, vWord


Range("A2").Select
While ActiveCell.Value <> ""
   vWord = ActiveCell.Value
   i = InStrRev(vWord, " ")
   vZip = Right(vWord, i)
   vST = Mid(vWord, i - 2, 2)
   vAddr = Left(vWord, i - 3)
   
   ActiveCell.Offset(0, 1).Value = vAddr
   ActiveCell.Offset(0, 2).Value = vST
   ActiveCell.Offset(0, 3).Value = vZip
   
   ActiveCell.Offset(1, 0).Select   'next row
Wend
End Sub


Nice try, but doesn't extract the city, just the state and the rest of the address after the number.
 
Upvote 0
Completely agreed with Special-K99.

Unless there's some way that you know of which will indicate whether, for a given string, the city to be extracted consists of one or two words, then I don't see how this can be achieved, apart from resorting to the method outlined by Special-K99 of looking up against a list of all towns/cities in the USA.

Regards
 
Upvote 0
I ran across something similar the other day. Using the answer format that I saw member: pgc01 use it will get you a little way down the road...


In cell B2 put this;

=TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),300))


IF you want to have an optional column grabbing that other word, perhaps a gray shaded column, I don't know...


In C2 put this;

=TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),400))


It was a neat approach...

Good Luck !!!
Chris
 
Upvote 0
As has been pointed out, without delimiters, there is no guaranteed way to split out the data (there are more problem areas than have been pointed out so far)... there is no code that can be absolutely accurate, however, the following code should get you (I would estimate) about 95% of the way there, but you will still have to scan the list by eye in order to try and spot anomalies. In particular, I pointed out a particularly harder-to-spot problem in the comment at the beginning of my code. Anyway, here is the macro...
Code:
[COLOR="#008000"]'  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.[/COLOR]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(Parts(X), ".", "")) & " ") > 0 Then
        For Z = X + 1 - (InStr(Unit, " " & UCase(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
@Rick,

For my purposes this works great. As you said, may get 95% and then the rest will be easy pickin'

Just ran 1 test on 77 rows of data and only one address did not come back quite right.

Thanks a million Rick and a big thank you to all
 
Upvote 0
@Rick,

Just ran 1 test on 77 rows of data and only one address did not come back quite right.

Thanks a million Rick and a big thank you to all
You are quite welcome, I am glad I was able to help you out.

Just out of curiosity, can you show me the address that my code missed (change the street number to 9999 and the Zip Code to 12345 so it is not a real address)?
 
Upvote 0
Thanks Rick,

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.
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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