+ Reply to Thread
Results 1 to 14 of 14

How would you extract the number the precedes a given word in a cell?

  1. #1
    Registered User
    Join Date
    12-08-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    7

    How would you extract the number the precedes a given word in a cell?

    The structure of my data (in each cell) is: Alpha Jan 13 35.00 Grams. So it is a record of an item (alpha in this cell), date (Jan 13), and number of grams. I need to find every cell that has this kind of record in a large data set, and after every occurrence of "Grams" I want to extract the number of grams. In this case 35.00. Note the place numbers are not equal. For example in another cell the record could be "Beta March 20 350.00 Grams".
    Last edited by Kimland; 12-08-2012 at 01:48 PM. Reason: SOLVED

  2. #2
    Forum Expert
    Join Date
    09-01-2012
    Location
    Norway
    MS-Off Ver
    Office 365
    Posts
    2,887

    Re: How would you extract the number the precedes a given word in a cell?

    Is the Text to Columns feature an option? It's on the Data tab.
    <----- If you were helped by my posts you can say "Thank you" by clicking the star symbol down to the left

    If the problem is solved, finish of the thread by clicking SOLVED under Thread Tools
    I don't wish to leave you with no answer, yet I sometimes miss posts. If you feel I forgot you, remind me with a PM or just bump the thread.

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,050

    Re: How would you extract the number the precedes a given word in a cell?

    this will extract the 35 (or 350) and convert it to a value...

    =VALUE(MID(A1,SEARCH(" ",A1,SEARCH(" ",A1,SEARCH(" ",A1,1)+1)+1)+1,SEARCH(".",A1,1)-(SEARCH(" ",A1,SEARCH(" ",A1,SEARCH(" ",A1,1)+1)+1)+1)))
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    12-08-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How would you extract the number the precedes a given word in a cell?

    That would work, but I a single step application would be preferable because I am working with dynamic data. Appreciated!

  5. #5
    Registered User
    Join Date
    12-08-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How would you extract the number the precedes a given word in a cell?

    FDibbins, that does it. Cheers!

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,050

    Re: How would you extract the number the precedes a given word in a cell?

    happy to help, and maybe consider point 3 below

  7. #7
    Registered User
    Join Date
    12-08-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How would you extract the number the precedes a given word in a cell?

    Good people, I have been trying to retrace this thread for a long time, but I thougt I had conducted it in email. I am glad to relocate it.

    Now on the same question, my data does vary in length and decimal places, and I need to read the number precisely as included in the dataset. How would you modify the code
    =VALUE(MID(A1,SEARCH(" ",A1,SEARCH(" ",A1,SEARCH(" ",A1,1)+1)+1)+1,SEARCH(".",A1,1)-(SEARCH(" ",A1,SEARCH(" ",A1,SEARCH(" ",A1,1)+1)+1)+1)))
    so that you read the number precisely, including the decimals, noting that some could be just one decimal places, others could be as long as 4 decimals? As the code is currently it returns 2 zeros for decimal places. Which entry of the code dictates how many places the code reads after the period? I am having difficulties catching up to the logic of programing in exel and the programs I am familiar with are currently unreachable - so I have to learn.

    I will appreciate your help.
    Last edited by Kimland; 12-10-2014 at 12:25 AM.

  8. #8
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,050

    Re: How would you extract the number the precedes a given word in a cell?

    wow, interesting seeing some old solutions here, thanks for that

    IF your real data format is consistent with your sample data...Name space month space day space amount space Grams (alpha month date amount grams) then here is a different approach
    =--SUBSTITUTE(MID(A2,FIND("xx",SUBSTITUTE(A2," ","xx",3),1)+1,99)," Grams","")

  9. #9
    Registered User
    Join Date
    12-08-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How would you extract the number the precedes a given word in a cell?

    Most appreciated FDibbins,

    But I made an error in making it seem like "grams" is universal in all fields. That's not the case. Your original formulary works really well, except for the decimal places, if you can find a way of updating that.


    Actually in the original problem 'grams' was universal, but now I have many other codes in the units column.

    Regards.
    Last edited by Kimland; 12-10-2014 at 01:45 AM.

  10. #10
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,050

    Re: How would you extract the number the precedes a given word in a cell?

    OK try this...
    =--LEFT(MID(A2,FIND("xx",SUBSTITUTE(A2," ","xx",3),1)+1,99),FIND(" ",MID(A2,FIND("xx",SUBSTITUTE(A2," ","xx",3),1)+1,99),1))

  11. #11
    Registered User
    Join Date
    12-08-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How would you extract the number the precedes a given word in a cell?

    Awesome! It does the job. I don't want to abuse the help, but I would be delighted if you would find the time to break down the code for my understanding. Many thanks!

  12. #12
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,050

    Re: How would you extract the number the precedes a given word in a cell?

    Sure.

    THis is the main part, it finds where the values start...
    MID(A2,FIND("xx",SUBSTITUTE(A2," ","xx",3),1)+1,99)

    I use SUBSTITUTE() to swap the 3rd space with xx, so it can be found
    SUBSTITUTE(A2," ","xx",3)
    FIND("xx",SUBSTITUTE(A2," ","xx",3),1)

    THen I use MID() to return a bunch of values from that position onwards. It doesnt really matter how many, at this point, I am just interested in everything from the value onwards
    MID(A2,FIND("xx",SUBSTITUTE(A2," ","xx",3),1)+1,99)
    the +1 after the FIND() is for the space that was found

    We now have something like this...
    35 grams

    All that remains is to find that space, and take everything to the left of it. So essentially, we have this..
    =--LEFT("350 grams",FIND(" ",MID(A2,FIND("xx",SUBSTITUTE(A2," ","xx",3),1)+1,99),1))

    Finally, because LEFT/MID/RIGHT return text, I used -- to convert that to a value

    Hope that helps?

  13. #13
    Registered User
    Join Date
    12-08-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: How would you extract the number the precedes a given word in a cell?

    Many thanks. Its very helpful.

  14. #14
    Registered User
    Join Date
    05-10-2013
    Location
    Pomáz, Hungary
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: How would you extract the number the precedes a given word in a cell?

    Give this formula a try too:

    =TRIM(MID(SUBSTITUTE(A2," ",REPT(" ",40)),120,40))

+ 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