Parse words out of string

Nuz

Board Regular
Joined
Aug 16, 2010
Messages
88
I have a string consisting of multiple words which I'd like to break down into separate words by VBA. For example the initial string is an address like "Street 66 123456 LONDON". I need to parse the city, postal code and the street out of that string and put them into separate variables.

The problem is that sometimes the string can be like "Street 66 123456 LONDON (CITY), i.e. the city is not one word but two or more. That's why for the postal code I can't always just find the second word from the right since it is not always the postal code.

I know that:
-the city is always the right-most word or words,
-postal code is always left of city
-street address is everything else and starts always from the left

Is there a foolproof way to parse out these words from the initial string?
 
I would put the process this way:

1) For postal code, give the first series of numbers (any number of digits) iterating from the right
2) For city, give everything that lies on the right side of the postal code
3) For address, give everything that lies on the left side of the postal code

For example, "Street 66 99 66 2345 LONDON" would be broken down to "Street 66 99 66" , "2345", "LONDON"
I was also thinking of a RegExp solution. Mine is a standard macro rather than a user-defined function. It assumes data starts in cell A2.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> ParseAddress()<br>    <SPAN style="color:#00007F">Dim</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    <SPAN style="color:#00007F">With</SPAN> CreateObject("VBScript.RegExp")<br>        .Pattern = "( \d+ )(?=\D+$)"<br>        Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Range("A2", Range("A" & Rows.Count).End(xlUp))<br>            s = c.Value<br>            <SPAN style="color:#00007F">If</SPAN> .test(s) <SPAN style="color:#00007F">Then</SPAN><br>                c.Offset(, 1).Resize(, 3).Value = Split(.Replace(s, "|$1|"), "|")<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> c<br>        Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>


Sample data and results:

Excel Workbook
ABCD
1
2Street 66 123456 LONDONStreet 66123456LONDON
3XYZ
4Street 66 123456 LONDON (CITY)Street 66123456LONDON (CITY)
562 North Street 66 00123456 LONDON (CITY)62 North Street 6600123456LONDON (CITY)
6Street 66 012345 LONDONStreet 66012345LONDON
7
842 8864 56 6549 New York City42 8864 566549New York City
9Street 66-99 012345 LONDONStreet 66-99012345LONDON
10Street 66 (Street 88) 123 LONDONStreet 66 (Street 88)123LONDON
11Street 66 / Street 88 012345 LONDONStreet 66 / Street 88012345LONDON
12Street 66 012345 LONDON (CITY)Street 66012345LONDON (CITY)
Parse words
 
Last edited:
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Both suggestions work perfectly.
That was something new for me. I haven't heard of RedExp before.

Very nice work!
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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