Extract varying lengths of text

N_Mitch

Board Regular
Joined
Jan 23, 2007
Messages
146
G'day,

I have a column of data that contains suburb, state and postcode (zip code). I can extract the postcode quite easily using:
Code:
=RIGHT(H2,4)

However, sometimes there is one word for the suburb, and other times there are two or more words. Also, the state abbreviation contains either two or three letters:
Code:
MARYBOROUGH QLD 4650
BRAY PARK QLD 4500
TEA TREE GULLY SA 5091

Does anyone know of any code I could use to extract the three pieces of information into three new columns? (I.e. Suburb, State, Postcode)

I imagine the formula would contain some method of counting the number of paces; however, the number of spaces between each word may be more than one space.

Thanks,

Mitch
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi,

In I2,

=TRIM(SUBSTITUTE(SUBSTITUTE(H2,J2&" ",""),K2,""))

J2,

=TRIM(LEFT(RIGHT(H2,8),3))

K2,

=RIGHT(H2,4)

HTH
 
Upvote 0
Hi Mitch,

The =FIND(" ",E30,1) formula will tell you the position of the spaces. You can use LEN, LEFT, MID and RIGHT formulas to extract info. There's more about the formulas in help. FIND will look from the left whereas it would be great for you if it looked from the end of the text.
Have you thought about "Data text to columns"? Try that then CONCATENATE the place names.
Another option is to download the postcode list from www.auspost.com.au and LOOKUP names for each postcode. Problem is, many postcodes have multiple place names but that's a start....

Good luck,
Bruce
 
Upvote 0
G'day Kris and Bruce,

Unfortunately, Kris' formula won't work, because the number of spaces between words in column H varies. For example, consider the following - each line represents a cell (H8 and H9):
Code:
MARYBOROUGH  QLD  4650
MARYBOROUGH   QLD   4650
As you can see, there are two spaces between each set of characters in cell H8, but three between each in cell H9.

I probably need a UDF that counts the number of spaces, because it seems as though the number of spaces is uniform within each cell (i.e. if there are two spaces between the first and second word, there will be two spaces between the second and third words). The UDF would then need to use these spaces as delimeters, then split the whole words into sets (i.e. starting from the end, the first set will be the Post Code - 4 characters - then the next set, being ether 2 or three characters, will be the State - and the remaining words will be the Suburb).

But I don't know how to put this into code.

Thanks,

Mitch
 
Upvote 0
Just an update:

Where there are two or more words making up the suburb name, then only one space exists between them; however, there are usually two spaces between the other words. While some states and post codes have three spaces between them, my prior statement of this being uniform within a cell still stands. IOW, if H8 has two spaces between the Suburb and State, there will be two spaces between the State and Post Code.

Any assistance would be appreciated,

Regards,

Mitch
 
Upvote 0
Almost Solved

Here is the result I came up with. While it is a work-around, it works for all cells in the column.

First, in I2 I have this formula:
Code:
=TRIM(RIGHT(H2,10))

This makes sure that regardless of whether there are two or three spaces between each word, only the State and Post Code are extracted. There will be at least 10 characters and spaces between the last Suburb name and the last character in the cell (11 characters where there are three spaces between these words).

In J2 I have this formula:
Code:
=LEFT(I2,3)

Last of all, here is the formula for K2:
Code:
=RIGHT(I2,4)

Thanks Kris and Bruce for contributing.

There is still one problem remaining - how to extract the suburb name(s)? Or how to remove the state and postcodes? I'll play around with the Substitute formula to see if that gives me any joy.

Regards,

Mitch
 
Upvote 0
Hi,

Change J2 formula like this:

Either one.

1. =TRIM(LEFT(RIGHT(SUBSTITUTE(H2,CHAR(160),""),8),3))

2. =TRIM(LEFT(RIGHT(TRIM(H2),8),3))

I2:

Either one.

1. =TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(H2,CHAR(160),""),J2&" ",""),K2,""))

2. =TRIM(SUBSTITUTE(SUBSTITUTE(H2,J2&" ",""),K2,""))

HTH
 
Upvote 0
Hi,

There is still one problem remaining - how to extract the suburb name(s)? Or how to remove the state and postcodes?

Go to Data > Text to Columns.. check delimited > check Spaces > Next > Finish.

HTH
 
Upvote 0
G'day Kris,

This works:
Code:
I2:
=TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(H2,CHAR(160),""),J2&" ",""),K2,""))

J2:
=TRIM(LEFT(RIGHT(TRIM(H2),8),3))

K2:
=RIGHT(H2,4)

Excellent! Thanks Kris!

(Edit: Make post more readable)
 
Upvote 0

Forum statistics

Threads
1,223,833
Messages
6,174,907
Members
452,590
Latest member
CraiginColorado

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