+ Reply to Thread
Results 1 to 23 of 23

Searching for a string, return sum

  1. #1
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Searching for a string, return sum

    This can't be a difficult one but I'm having a memory blank!

    I have several cells in Column A containing text. In Column B the cell contains a value. I wish to return the sum of the total value if Column A contains a specific word. In other words, I need to search Column A for a string, if that is found return the sum of the values in Column B.

    Hope someone can advise and prompt my mind back into action!

    BT.

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Searching for a string, return sum

    So the string in Col A can be part of a string? i.e.
    Housefly---- 12
    House------ 4
    In the House--2
    living room ---10
    will return 18 or just 2?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    It could be any part of the string, so in your example it would need to return the sum of 18.

  4. #4
    Registered User
    Join Date
    06-29-2010
    Location
    Central Texas
    MS-Off Ver
    Excel 2007
    Posts
    33

    Re: Searching for a string, return sum

    Assuming the above example are in column "A" and "B" then this should work
    =SUM(--(IFERROR(IF(SEARCH("house",A3:A6),1,0),0))*(B3:B6)) and enter as an array formula with CTRL, Shift, Enter.

  5. #5
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Searching for a string, return sum

    This should work also
    =SUMPRODUCT(--(ISNUMBER(SEARCH("house",$A$1:$A$4000))),$B$1:$B$4000)

    It does not require being entered as an Array.
    Last edited by ChemistB; 07-13-2010 at 03:09 PM.

  6. #6
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    Thanks for trying to help as well. I have copied your formula and just changed where necessary but no value is returned. Would it cause a problem if the values I am trying to return are formatted as a currency in the cell rather than a number?

    Ben.

  7. #7
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Searching for a string, return sum

    Being formatted as currancy should not affect the formula. Without an example workbook (enter dummy data if necessary), it's hard to troubleshoot. (Go advanced and "Manage Attachments").

    One thought, if you modified the formula. the ranges must be the same size (i.e. can't have A2:A4000 and B2:B1000).

  8. #8
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    Attached is a sub-set of what I'm trying to work on.
    Attached Files Attached Files

  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: Searching for a string, return sum

    err what value/string you looking for?
    but
    =SUMIF(A2:A5,"*"&D1&"*",B2:B4) where d1 contains word pennant would give
    10.93
    Last edited by martindwilson; 07-13-2010 at 04:14 PM.
    "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

  10. #10
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    I'm looking to return the sum of the price in the second column where the string in the first column contains the word 'pennant'.

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

    Re: Searching for a string, return sum

    see my last post
    or just
    =SUMIF(A2:A5,"*pennant*",B2:B4)
    @chemistb i thought you were tied up on that other thread lol
    Last edited by martindwilson; 07-13-2010 at 04:17 PM.

  12. #12
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Searching for a string, return sum

    LOL, I have to get up and walk around occasionally.

  13. #13
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    Thanks for looking into this Martin.

    I've tried both of your formulas. The first just returns a '#NAME?' error.
    The second example you provided has returned '0' this time. So no error is coming up but at the same time the sum doesn't seem to be calculating and returning any value. Any idea??

    Just to clarify, the word 'pennant' could be located anywhere within the cells that I'm searching for in Column A.

    Ben.

  14. #14
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Searching for a string, return sum

    #Name error probably means your quotations are not set properly? Your text needs to be in double quotes.

  15. #15
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    I've set double quotes around the word like so:

    =SUMIF(A2:A133,'*'&pennant&'*',B2:B133)

    but it returns:

    #NAME?

  16. #16
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Searching for a string, return sum

    =SUMIF(A2:A133,"*pennant*",B2:B133)

  17. #17
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    Ok, that doesn't return an error now. However, it still only returns zero (0) and not the sum of the values where the word 'pennant' occurs.

    This is weird!!

  18. #18
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Searching for a string, return sum

    See attachment
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    Thanks for that. I've taken your example and amended it slightly but the function still doesn't return a value. Attached is what I've done - you will see that each cell just returns £0.00.

    Any thoughts?
    Attached Files Attached Files

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

    Re: Searching for a string, return sum

    not when i open it it shows £34.53
    Last edited by martindwilson; 07-13-2010 at 06:18 PM.

  21. #21
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    Ok, I think I've tied the problem down now.
    I'm using Open Office to work .xls files and there must be a different character used instead of the *

    So in the example below I need to replace the * with something else. It's not a '%' sign though!!
    =SUMIF(A2:A133,"*pennant*",B2:B133)


    I'll have a search to see if I can find out what to use.
    Thanks for all your help!

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

    Re: Searching for a string, return sum

    Dont you think you should have mentioned open office?. Calc is not the same as excel
    calc uses regular expressions and wild card is .* to get it to work you need
    =SUMIF(A$2:A$13;".*pennant.*";B$2:B$13) then go to tools/options/openoffice.orgcalc/calculate
    and check enable regular expressions in formulae click ok
    Last edited by martindwilson; 07-13-2010 at 07:30 PM.

  23. #23
    Registered User
    Join Date
    06-10-2010
    Location
    Berkshire, England
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    67

    Re: Searching for a string, return sum

    Many thanks for all your help Martin. It's much appreciated and apologies for not being clear from the outset.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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