+ Reply to Thread
Results 1 to 14 of 14

Extract Data from a string

  1. #1
    Registered User
    Join Date
    12-04-2008
    Location
    Birmingham, UK
    MS-Off Ver
    Office 2007
    Posts
    43

    Extract Data from a string

    I am using Excel 2007 and I have a workbook with 12 different sheets. I have attached a sample of the field I need help with.

    I need to extract the City/Town out of the branch field. The only pattern I can spot is that each branch begins with a number which is up to 4 numeric characters long, followed by a space and then the first part of a City/Town. This is followed by a space and either the second part of the town or another piece of information which I don't need.

    I appreciate any help anyone can give me, either a formulae or VBA.
    Attached Files Attached Files
    Last edited by dcaraher; 12-15-2010 at 10:13 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,290

    Re: Extract Data from a string

    One way, in cell B2:

    =IF(ISERROR(FIND(" ",A2,FIND(" ",A2)+1)),MID(A2,FIND(" ",A2)+1,LEN(A2)),MID(A2,FIND(" ",A2)+1,FIND(" ",A2,FIND(" ",A2)+1)-FIND(" ",A2)-1))

    This is basically looking for the two spaces and calculating and extracting the number of characters in between.

    The first part, "IF(ISERROR(FIND(" ",A2,FIND(" ",A2)+1))" is checking to see if there is a *second* space; if not, you will get an error which is picked up by the ISERROR function.

    If there is an error, the second part (the TRUE part of the IF statement), "MID(A2,FIND(" ",A2)+1,LEN(A2))", gives you the characters after the only space.

    The third part (the FALSE part of the IF statement), "MID(A2,FIND(" ",A2)+1,LEN(A2)),MID(A2,FIND(" ",A2)+1,FIND(" ",A2,FIND(" ",A2)+1)-FIND(" ",A2)-1)", gives you the characters between the two spaces.

    Drag down to cell B103.


    Regards
    Last edited by TMS; 12-15-2010 at 05:53 AM.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    12-04-2008
    Location
    Birmingham, UK
    MS-Off Ver
    Office 2007
    Posts
    43

    Re: Extract Data from a string

    TM, thanks for your help with this. It is very close to what I need, and perhaps I am asking for the impossible.

    Your formulae only extracts the first part which is fine for rows 1 , 2 & 3 but in row 4 it picks up Glasgow-Graham. I guess this is because the delimiter is a "-".

    Also in row 21 it produces a result of High where I need it to produce High Wycombe.

    Again many thanks foryour help so far, and it may be that what I want is not possible.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,290

    Re: Extract Data from a string

    The only pattern I can spot is that each branch begins with a number which is up to 4 numeric characters long, followed by a space and then the first part of a City/Town. This is followed by a space and either the second part of the town or another piece of information which I don't need.
    I thought it did what you described. I perhaps misinterpreted "the second part of the town". You may need to undertake a data cleansing exercise, at least for the "-" part of the problem. With regard to the name of a town, I'm not sure what you're going to do about that.

    I suspect that you would have the same issue with a VBA solution.

    Maybe someone else can help; I will be interested in the answer.

    Regards

  5. #5
    Registered User
    Join Date
    12-04-2008
    Location
    Birmingham, UK
    MS-Off Ver
    Office 2007
    Posts
    43

    Re: Extract Data from a string

    TM having re-read my initialrequest I can see how I didn't make it clear.

    I think this may be too difficult because even if I replace the "-" delimiter with a space, I am still asking for a solution to decide whether the second part of the string is required or not.

  6. #6
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extract Data from a string

    data is not consistent eg kings norton how would you know thats not just kings
    you could compare each against a list of uk towns cities but even kings norton isnt in those lists as its an area of birmingham so youd need cities/towns/ district /borough lists
    if you had the associated post codes it might be easier to extract from that but I'm not sure.
    another example is glasgow hillington so why not london penge?
    Last edited by martindwilson; 12-15-2010 at 06:30 AM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  7. #7
    Registered User
    Join Date
    12-06-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Extract Data from a string

    I've attached the solution to the spread sheet you provided. Quite simple really. I used the TEXT functions: LEN and MID.
    Attached Files Attached Files

  8. #8
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extract Data from a string

    @ LL1980 all that does is remove the leading digits! and even then would fail if there were more than 4 or less than 3
    the same thing you show can be accomplished with just
    =MID(A2,FIND(" ",A2)+1,255)
    it doesnt parse 144 Lytham St Annes-Graham to just Lytham St Annes
    Last edited by martindwilson; 12-15-2010 at 06:56 AM.

  9. #9
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extract Data from a string

    i think perhaps as a prelimary move
    just find/replace *graham* with nothing first
    then find replace (* with nothing
    then find replace *p&h* with nothing
    then have a look whats whats left

  10. #10
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Extract Data from a string

    Hi dcaraher
    maybe...
    Please Login or Register  to view this content.
    you can probably do the same with out regular expresion
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  11. #11
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Extract Data from a string

    TMShucks
    looks like the address names are like surnames with too many variations to able to filter all of them correctly

  12. #12
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Extract Data from a string

    This UDF is a compromise but does thin the list fairly drastically.
    Please Login or Register  to view this content.

    Add to the array of "company names" as examples are found.
    And possibly add if statements to cover other delimiters as they reveal themselves.

    The end result should allow you to choose whether the text is an address or a town name.

    This is by no means ideal but might help some.
    It might help with your geography if nothing else.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  13. #13
    Registered User
    Join Date
    12-04-2008
    Location
    Birmingham, UK
    MS-Off Ver
    Office 2007
    Posts
    43

    Re: Extract Data from a string

    Guys thanks very much for all of your help, this is a perfect example of why this board is so helpful to excel novices like me.

    I found an alternative solution to my problem. I extracted teh branch number using the LEFT function. I then went to the originator of the file and asked for a branch list and used that in a VLOOKUP. Not an elegant solution but at least it fixed my immediate problem.

    However looking through the suggested solutions has increased my knowledge so a double success.

    Once again many thanks for your help.

  14. #14
    Registered User
    Join Date
    12-04-2008
    Location
    Birmingham, UK
    MS-Off Ver
    Office 2007
    Posts
    43

    Re: Extract Data from a string

    Never give up........lol

    Actually the UDF from Marcol worked very well, and I think with a little adaptation I could use it for other similar problems.

    Thanks everyone.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1