+ Reply to Thread
Results 1 to 11 of 11

Extracting numbers from text and summing

  1. #1
    Registered User
    Join Date
    07-21-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    7

    Extracting numbers from text and summing

    Hello. I would appreciate if someone could help me on this. I have a column in a spreadsheet for numbers entered in each age group text box that is concatenated into a cell. Some age groups may not have anything entered for them but the age group title is still concatenated into the cell. My problem now is to take all the numbers entered for each age group in each cell, and then separately add them. I've also attempted this separation and total in each age group with separate columns. Here is the example of what I'm doing to attempt to accomplish this:

    "0-3 years old: 1
    4-7 years old:
    8-11 years old:
    12-15 years old:
    16-19 years old:
    20 years old and over: "

    Below are the separate columns I'm using to attempt to separately add the numbers of each age range. This is the formula to try to add the number in the 0-3 years old age range. =SUMIF($C$2:$C$250, RIGHT(ISNUMBER, $C$2:$C$250("0-3 years old:")))



    Age Ranges Count By Age
    0-3 years old: 0
    4-7 years old: 0

  2. #2
    Forum Contributor
    Join Date
    07-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    104

    Re: Extracting numbers from text and summing

    Can you create a fake spreadsheet and show what you want to happen?

  3. #3
    Registered User
    Join Date
    07-21-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    7

    Re: Extracting numbers from text and summing

    I just tried a SEARCH function and it didn't do what I want but I figured out what it counted. Here is an example formula
    =SEARCH("0-3 years old:", $C$2:$C$250). This returned a 1. I did it for the next age range in each cell and it returned a 17. I figured out it was counting up to the first position of the string requested in the SEARCH. So for instance, if I could Search for the number entered for that age group by referencing the number of characters it is located in the string and then add that number entered in each age group. The number of kids in age group 0-3 years old would be located at the 15th or 16th position if that clarifies it.

  4. #4
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Extracting numbers from text and summing

    Welcome to the forum Shaw2380.

    The easiest way to deal with this would be to not have the information in one cell. You say it's 'concatenated' which implies to me that the information is taken from somewhere else - is this the case, or is it supplied to you in this format?

    If there's nothing you can do about the format, then the easiest (relatively speaking ) way to deal with this is to put the numbers for each age group into a separate column, using MID and RIGHT functions.

    If you have your information in A2, put these formulae in B2 to G2, to extract the different age groups numbers:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Short explanation of each part of the formula:
    IFERROR(...,0) gives you a 0 if there's nothing entered for that age group.
    -- converts the rest of the result from text to a number (it's equivalent to using VALUE(...)
    CLEAN strips out the non-printing carriage return which separates the lines in the first cell
    MID/RIGHT with FIND with SUBSTITUTE looks up the part of the cell between the first/second/etc colon : and the next age group (if you want more explanation of this bit, just ask)


    Drag the formulae down to extract from A3, A4, etc.
    You can then simply Sum the numbers.

    The attached file shows an example of this working.

    Hope that does what you want.

    Edit: just saw your post about SEARCH. You're on the right lines thinking about position in the string - that's what the FIND part of the formulae above does (that is, return the position of what it finds). In fact, since FIND is case-sensitive and SEARCH isn't, if there's a chance that you might get data with '4-7 YEARS' instead of '4-7 years' then you should replace the FIND in my formulae with SEARCH.
    Last edited by Aardigspook; 07-21-2017 at 02:53 PM. Reason: Note previous post
    Regards,
    Aardigspook

    I recently started a new job so am a bit busy and may not reply quickly. Sorry - it's not personal - I will reply eventually.
    If your problem is solved, please go to 'Thread Tools' above your first post and 'Mark this Thread as Solved'.
    If you use commas as your decimal separator (1,23 instead of 1.23) then please replace commas with semi-colons in your formulae.
    You don't need to give me rep if I helped, but a thank-you is nice.

  5. #5
    Registered User
    Join Date
    07-21-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    7

    Re: Extracting numbers from text and summing

    Do you have to do that formula for each and every cell in the sheet or could I do something like $C$2:$C$250? I'll try and see if I can modify the code example and if it doesn't work that way will setup how you did it.

  6. #6
    Registered User
    Join Date
    07-21-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    7

    Re: Extracting numbers from text and summing

    Tried it and got 0s even testing in a cell that had a number for 0-3 years old: I tested with =IFERROR(--CLEAN(MID($C$2:$C$250,FIND("^",SUBSTITUTE($C$2:$C$250,":","^",1))+1,FIND("^",SUBSTITUTE($C$2:$C$250,"0-3 years old","^"))-FIND("^",SUBSTITUTE($C$2:$C$250,":","^",1))-1)),0) and also tested by replacing $C$2:$C$250 with a cell that has a number entered for that age group but still shows nothing. I see from your sheet it works for yours. Could it because I have Excel 2016?

  7. #7
    Forum Contributor
    Join Date
    07-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    104

    Re: Extracting numbers from text and summing

    Quote Originally Posted by Shaw2380 View Post
    Do you have to do that formula for each and every cell in the sheet or could I do something like $C$2:$C$250? I'll try and see if I can modify the code example and if it doesn't work that way will setup how you did it.
    If you could provide a sample spreadsheet showing what your data looks like and what you expect the result to look like, there may be other solutions.

  8. #8
    Registered User
    Join Date
    07-21-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    7

    Re: Extracting numbers from text and summing

    What is the "^" for? Could that be the problem since I don't have ^ anywhere in the cells?

  9. #9
    Registered User
    Join Date
    07-21-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    7

    Re: Extracting numbers from text and summing

    I finally was able to figure it out and adapt your code to what I need. Thank You so much. You are a miracle worker.

  10. #10
    Registered User
    Join Date
    07-21-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    7

    Re: Extracting numbers from text and summing

    Sorry. One last question. I got all of the age groups except 20 years old and older. I figure it to be that since for each age group you put the next age group in parenthesis and since 20 years old and older has no age group I tried "" since no age group after it to reference.

  11. #11
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Extracting numbers from text and summing

    The answer to your question is basically the explanation of how the MID/RIGHT, SUBSTITUTE, FIND/SEARCH works.

    MID is used to find something in the middle of the contents of a cell. The syntax is:
    MID( text/cell , number of starting character , number of characters ). For example, if you have 'text123' in A1, the formula MID(A1,4,3) will give you 't12'.
    RIGHT does the same but at the right-hand end of a cell. The syntax is:
    RIGHT ( text/cell , number of characters ). For example, with the same A1 as above, RIGHT(A1,2) will give you '23'.

    That's why MID is used for all the age groups except the last. For the 20+ age group, the number is at the end, so RIGHT works fine for it.
    For both of them you need to find the numbers of the positions of various characters.

    That's where FIND (case-sensitive) or SEARCH (not case-sensitive) come in. Other than the case-sensitivity, they do the same thing - I'll just refer to FIND from now on.
    The syntax is:
    FIND ( text to find , in text/cell ) and will return the position of the first character of the 'text to find'. So, with the same 'text123' in A1, FIND("x",A1) will return '3', FIND("xt",A1) will also return '3', FIND("t1",A1) will return '4'.
    Obviously (I hope) you can use this to get a starting character number for MID. You can also subtract one FIND result from another to get the number of characters you want.
    So, MID(A1, FIND("t1",A1) , FIND("t1",A1)-FIND("e",A1)) becomes MID(A1,4,4-2) then MID(A1,4,2), which gives 't1'.

    So, with your text, you could use FIND with a colon and you'd get the first position of the first colon.
    Adding one gives you the position of the next character - which is probably a space, but could be the start of the number of 0-3 year-olds.
    FIND with the text '4-7 years' gives the position of the '4' from that text. You could then put these together into your first MID:
    MID ( A2 , position of colon +1 , position of '4-7 years' – (pos of colon +1) )

    I'm sure you've already spotted the problem - how do you find the second colon, third colon, etc. That's where SUBSTITUTE fits in. This is used to Substitute one thing for another. The syntax is SUBSTITUTE( in text/cell , old text , new text , [optional - instance number] ). The old text/new text don't need to be the same length.
    So, with 'text123' in A1, SUBSTITUTE(A1,"12","33") will return 'text333', or SUBSTITUTE(A1,"text","something") will give 'something123'.
    The optional instance number allows you to replace the second, third, etc instance of something instead of the first one. So if you have 'text123:text456:text789' in A1, whilst the basic SUBSTITUTE(A1,"text","something") would give 'something123:text456:text789', using SUBSTITUTE(A1,"text","something",2) will give 'text123:something456:text789'.

    So, to find the second, third, etc instance of something, you first replace it with something you don't have anywhere in your text (^ is often good, or ~ or something like that). You then search for that - in this sort of form:
    FIND ( "^" , SUBSTITUTE ( A1 , ":" , "^" , 2 ))
    This will replace the second colon with a ^ then return the position of that - which is equivalent to the position of the second colon.
    Note that there's no actual replacement - it's just a virtual replacement inside the formula.


    Overall, the logic for the MID formula I gave you for 4-7 years old (for example) works something like this:
    We need to find the numbers between the second colon (the one after '4-7 years old') and the start of the phrase '8-11 years old'.
    The position of the second colon is found by Substitute of the second instance of ":" with "^". +1 gives the position of the character after it - which is the starting character we want for the MID formula.
    The number of characters is calculated by finding the position of the first character of '8-11 years' and subtracting the position of the second colon+1 (actually I subtracted the position of the second colon, then subtracted 1, which has the same effect: 10-(7+1) is the same as 10-7-1 ).

    Let's look at it using an example. First I'll number the characters:
    Please Login or Register  to view this content.
    So the second colon is in position 31 and the '8' of '8-11 years' is in position 36. The MID formula is therefore MID(A2,31+1,36-31-1) = MID(A2,32,4) which gives a result of:
    44 (with spaces before and after)

    As explained in my previous post, CLEAN then removes the non-printing characters to give just '44'.


    I know this has been an extremely long explanation , but hopefully it explains:
    1. why there are ^ symbols in the formula when they're not in your text (they're used in SUBSTITUTE);
    2. why there's no need for a 'higher age group' for the 20+ age group (RIGHT doesn't need to find anything further).

    Hope that helps.
    If I've just confused things horribly and something still isn't working, please just let me know!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Extracting numbers from text in cell range and summing them up using SUMPRODUCT
    By Jakub2 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-14-2017, 07:41 AM
  2. [SOLVED] Extracting numbers from text?
    By shiftyspina in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 01-15-2014, 11:10 AM
  3. [SOLVED] extracting numbers from a string of text and numbers
    By ScottLor in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-05-2013, 04:47 PM
  4. Extracting numbers and text from inconsistent text/number formatted string
    By Brandivil in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-25-2013, 01:46 PM
  5. [SOLVED] Extracting Numbers from text
    By hoventim in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 12-11-2012, 09:08 AM
  6. Replies: 17
    Last Post: 03-03-2010, 06:55 PM
  7. Extracting numbers from text
    By bmind in forum Excel General
    Replies: 6
    Last Post: 02-23-2010, 11:16 PM

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