+ Reply to Thread
Results 1 to 11 of 11

Calculate the sum of items in one column if condition other column = true

  1. #1
    Registered User
    Join Date
    12-22-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    10

    Calculate the sum of items in one column if condition other column = true

    Hi everybody,

    I've got a problem I can't find the solution for, if there is one. I want to calculate the sum of all the items in column1 that match a certain condition in column2.

    You can see an example of what I mean in the attached file. As you can see I've solved the problem by adding an additional column for each category, but I don't think this is a very elegant way of doing this. It also poses some problems, e.g. the formulas don't always automatically get copied when I insert a new row etc.

    So, my question is, can I write a formula like this in one single cell (not really an accurate piece of code, but I hope you understand what I mean):
    Sum(
    for i=1 to i=infinity
    if(Ai=cat1;Bi;"")
    )

    I guess that's impossible without using macros? Has anybody got an idea? And if it's only possible with macros, how would I write one. I've got basic general programming skills (although it's been quite some time now), but I haven't written a macro before.

    Thanks in advance for any help,

    Eddy.
    Attached Files Attached Files
    Last edited by Eddy Reddy; 12-22-2010 at 06:54 AM.

  2. #2
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Calculate the sum of items in one column if condition other column = true

    Hi and welcome to the board

    Maybe
    Please Login or Register  to view this content.
    for cat 1 ?

    Replace 1 with the cat nr you need, or eventually with a celle reference in which you can enter the desired cat nr

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,601

    Re: Calculate the sum of items in one column if condition other column = true

    Maybe this:

    =SUMIF(A:A, 1, B:B)

    For 1, 2, 3... you can use:

    =SUMIF(A:A, rows($A$1:A1), B:B) and pull down

  4. #4
    Forum Contributor
    Join Date
    01-28-2010
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    157

    Re: Calculate the sum of items in one column if condition other column = true

    Total Cat. 1=SUMIF(A:A,1,B:B)
    Total Cat. 2=SUMIF(A:A,2,B:B)
    Total Cat. 3=SUMIF(A:A,3,B:B)

    Beau Nydal

  5. #5
    Registered User
    Join Date
    12-22-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calculate the sum of items in one column if condition other column = true

    Thank you both. This is exactly what I meant. I didn't know the function SUMIF.

    Thanks again and happy holidays,

    Eddy.

  6. #6
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Calculate the sum of items in one column if condition other column = true

    Happy Holidays to you too, wherever you are in this snowy country of ours :-)

  7. #7
    Registered User
    Join Date
    12-22-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calculate the sum of items in one column if condition other column = true

    Hi again,

    I seem to have encountered a new problem. In the actual document I need this for, I've got multiple conditions. I've googled and found the function SUMPRODUCT.

    The previous condition SUMIF(A:A, 1, B:B) should only be calculated when there is no value in column M. So I come to this formula:
    =SUMPRODUCT(--(M:M<>"");--(A:A=1);--(B:B))

    But this results in #NUM!. However, I can't exactly see what I did wrong.

    Also, is it possible to use other functions like ISBLANK in addition to SUMPRODUCT or IFSUM?

    Thanks in advance,

    Eddy
    Last edited by Eddy Reddy; 12-22-2010 at 06:48 AM.

  8. #8
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,601

    Re: Calculate the sum of items in one column if condition other column = true

    First of all, you need to keep your ranges at minimum because SUMPRODUCT can be very slow...

    If your numbers in B column are stored as numbers then you can use this:

    =SUMPRODUCT(--(M1:M100="");--(A1:A100=1);B1:B100)

    and that should work (same as above that you already write)... But you should not have characters in B column.

  9. #9
    Registered User
    Join Date
    12-22-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calculate the sum of items in one column if condition other column = true

    Thanks again. I forgot the titles in row1 are also included in A:A, B:B and M:M.

    Cheers,
    Eddy.

  10. #10
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Calculate the sum of items in one column if condition other column = true

    And XLversions prior to 2007 do not admit entire columns ( like A:A) for the sumproduct function

  11. #11
    Registered User
    Join Date
    12-22-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Calculate the sum of items in one column if condition other column = true

    Ok, thanks. I also didn't think about the extra processing power needed, so I guess it's better this way anyhow.

    Greetings,
    Eddy

+ 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