Text Manipulation

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
586
Office Version
  1. 2013
Platform
  1. Windows
I am trying to strip of the postcode /zip code and put in a cell on its own can someone advise the bust why to do this data below.

Excel Workbook
A
5Aberdeen AB11 8HX
6Acocks Green B27 7UW
7Acton W3 8HH
8Aldershot GU11 1JG
9Altrincham WA14 5PZ
10Ashford TN24 0SP
11Aylesbury HP20 1JH
12Banbury OX16 2RL
13Bangor LL57 4SU
14Basildon SS14 3GD
15Basingstoke RG21 8BJ
16Bath BA1 3AG
17Beckenham BR3 4TJ
18Bedford (Midland) MK40 1QH
19Bedford (St John's) MK42 0XE
20Bedminster BS3 3PZ
21Belshill ML4 1BA
22Birmingham (Perry Barr) B44 8AA
23Blackburn BB2 3DY
24Bolton BL2 1HE
25Bournemouth BH8 8BD
26Bradford BD5 7JL
27Braintree CM77 8YH
28Bridgwater TA6 4DB
29Brighton BN1 4GD
30Bristol (Downend) BS15 1SE
31Bristol (Filton) BS34 7JU
32Bristol (Newfoundland) BS2 9AP
33Burntwood (UK Autocare) WS7 3TQ
34Burton DE14 1SN
35Bury St Edmunds IP33 1XP
36Camberley GU15 3EJ
37Camborne TR15 3PS
38Cambridge (Clifton) CB1 7EA
summary


thank you in advance
 
Excel Workbook
ABCD
1Smith, Fred MrMrFredSmith
One Way would be..... Sheet1
Excel 2003
Cell Formulas
RangeFormula
B1=TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",255)),255))
C1=MID(A1,FIND(",",A1)+2,FIND(" ",A1,FIND(",",A1)+2)-FIND(",",A1)-2)
D1=LEFT(A1,FIND(",",A1)-1)
 
Last edited:
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Sandeep many thanks. I know its a cheeky thing to ask, but wouldo you mind explaining how it works? :wink:
 
Last edited:
Upvote 0
Lets start with D1 (IMHO the easiest :))

LEFT(A1,FIND(",",A1)-1)

The text in A1 is Smith, Fred Mr

Take the inner function - FIND(",",A1) - This will find the position of "," in A1 => Smith, => 6

We then subtract 1 from this => 5

So the main formula becomes

LEFT(A1,5) => return 5 characters from A1 starting from the left => Smith

Now the formula in B1

TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",255)),255))

This is a very common method to extract the last word in a string.

Take the inner function => REPT(" ",255) => This just returns a string of 255 spaces.

SUBSTITUTE(A1," ",string of 255 spaces) => replaces all spaces in the text with 255 spaces

RIGHT(string with all spaces replaced by 255 spaces,255) => returns a string of 255 characters starting from the right => includes the last word in the text + a whole lot of spaces

TRIM(last word in the text + a whole lot of spaces) => last word in text => Mr

Now we move to C1

MID(A1,FIND(",",A1)+2,FIND(" ",A1,FIND(",",A1)+2)-FIND(" ",A1)-2)

The syntax for the MID function is MID(Text, Start Number, Num of Characters)

lets look at start number

FIND(",",A1)+2 => This finds the position of "," in the text => 6 as established earlier => 6+2 => 8

If we do a MID(A1,6,1) we will get "F"

lets look at num of characters

FIND(" ",A1,FIND(",",A1)+2)-FIND(" ",A1)-2

The syntax of FIND is FIND(Text, Within Text, [Start Number]) => Start Number is optional and by default is 1. So you can leave it blank and it'll assume 1 i.e. start from the beginning. However you can specify the start number so that FIND will start looking after the number specified. This is especially useful if you have text with 2 spaces and you want the position of the 2nd space.... so you have FIND(" ",A1,Position of 1st space + 1) which will begin FIND after the 1st space => the 2nd space.... coming back to our problem

FIND(" ",A1,FIND(",",A1)+2)-FIND(" ",A1)-2

=> FIND(" ",A1,8)-FIND(" ",A1)-2
=> 12-FIND(" ",A1)-2

If you leave out the red part you get MID(A1,8,12) => return text that is 12 characters long starting from the 8th character => Fred Mr => incorrect.

To get correct results you need to subtract the position of the 1st space from the position of the 2nd space
(I have replaced space with @ below to make it easier to understand)

Smith,@Fred@Mr => 12
Smith,@Fred@Mr => 8

12 - 8 = 4

=> 12-FIND(" ",A1)-2
=> 12-6-2
=> 12-8
=> 4

So the initial formula becomes

MID(A1,8,4) => Return a text that is 4 characters long starting from the 8th character => Fred

Hope this helps!

Also note that the formula in C1 will work only if there is one word between the "," and Mr (of course it can be extended to include 2-3-4 etc words, but that is where the pattern/format of text comes in.... which is what I was asking in the beginning)
 
Upvote 0
Wow! I've read it and read it and read it and I'm hoping some will stick!
The reason I asked was because I felt that by understanding I might be able to put something together to do all I need. I know understand I cannot. So, I'll just have to go through a series of formulae(?) to grab the last word.

It just occurs to me. Is it possible to set up a drop down list of formulae so one can choose whether to pick off say the first or last word?
 
Upvote 0
.....I know understand I cannot....

I'm sure you'll be able to understand. I used to find this very complicated when I first came across it. Try using the Evaluate Formula option that Excel provides. It helped me a lot in understanding many formulae.

Again, if you are able to detail out your requirements (with some sample data), I or someone else may be able to solve the problem.
 
Upvote 0
............Is it possible to set up a drop down list of formulae so one can choose whether to pick off say the first or last word?

To pick up any word within a string.

……A…...….…....................B……….…..C….
List of Data…...…….........word no…..Result….
Acocks Green B27 7UW……..2………...Green….

A2, enter : Acocks Green B27 7UW

B2, enter : 1 or 2 or 3 or 4 ( or, dropdown list with 1 to 4 )

C2, formula :

=TRIM(MID(SUBSTITUTE(A2," ",REPT(" ",30)),30*(B2-COLUMN(A2))+1,30))

Regards
Bosco
 
Upvote 0
Many thanks Bosco. I'll give that a try.
icon14.gif
 
Upvote 0

Forum statistics

Threads
1,216,052
Messages
6,128,509
Members
449,455
Latest member
jesski

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