Share via

How to tell excel to keep summing up values when a condition is met

Anonymous
2024-01-10T03:39:45+00:00
COLUMN A COLUMN B Result needed (column C)
0 0
8 0
7 1 7
6 0
5 1 6
4 1 5
5 0
4 0
2 1 4
0 1 3
3 1 2
2 0
9 1 1
5 1 5
4 0
3 0
2 1 4
1 1 3
0 1 2
9 1 1
8 0
7 1
6 1
5 1

Column A & B are the inputs. I want excel to check column A and whenever there is a number "9", then column C should sum up the "1s" in column B, and every time there is a number "9" in column A, the summation in column C should start over.

Please help

Thanks

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

7 answers

Sort by: Most helpful
  1. Anonymous
    2024-01-10T04:33:37+00:00

    Hi SaraHenry,

    Thanks for contacting us,

    To achieve the result you want, you need to use a formula that can sum up the values in column B based on the condition in column A. One possible formula is the SUMIF function, which can sum the values in a range that meet a certain criteria.

    However, the SUMIF function alone is not enough, because you also need to reset the sum whenever there is a number "9" in column A. To do that, you need to use another function that can count how many times the number "9" appears in column A up to the current row. One possible function is the COUNTIF function, which can count the number of cells in a range that meet a certain criteria.

    By combining the SUMIF and COUNTIF functions, you can create a formula that can sum up the values in column B based on the condition in column A and reset the sum whenever there is a number "9" in column A. Here is an example of such a formula:

    =SUMIF (A$2:A2, "<>9", B$2:B2) - SUMIF (A$2:A2, "<>9", B$2:B2) * COUNTIF (A$2:A2, "=9")

    This formula works as follows:

    • The first SUMIF function sums up the values in column B from row 2 to the current row, excluding the rows where column A is "9".
    • The second SUMIF function does the same thing, but multiplies the result by the number of times the number "9" appears in column A from row 2 to the current row, using the COUNTIF function.
    • The formula then subtracts the second SUMIF result from the first SUMIF result, effectively resetting the sum whenever there is a number "9" in column A.

    To use this formula, you need to enter it in cell C2 and then copy it down to the rest of the column. You can use the fill handle or the keyboard shortcut Ctrl+D to do that. You should see the result you want in column C.

    Let me know if this helps or if you need further assistance.

    Regards, Sola

    “Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below. “

    Kindly note that this is a user to user forum, we are users helping other users, we aren't Microsoft employee neither are we Microsoft agents.

    1 person found this answer helpful.
    0 comments No comments
  2. Ashish Mathur 101.8K Reputation points Volunteer Moderator
    2024-01-11T23:16:55+00:00

    You are welcome. If do not understand your requirement at all.

    0 comments No comments
  3. Anonymous
    2024-01-11T16:00:11+00:00

    Hi Ashish,

    Thank you for your help, this works well for when the "9" and the "1" meet in columns A and B. However there is a case that i forgot to mention where the 1 starts after the 9. as in the example below

    in summary, column C should start counting the 1s only if two conditions are true:

    CONDITION 1

    A13=9 and B=13

    A B C Result needed
    4 0
    2 1 4
    0 1 3
    3 1 2
    2 0
    9 1 1
    5 1 5
    4 0
    3 0
    2 1 4
    1 1 3
    0 1 2
    9 1 1
    8 0
    7 1 6
    6 1 5
    5 1 4
    4 0
    2 1 3
    0 1 2
    3 0
    2 0
    9 1 1

    CONDITION 2

    example: A13=9 and (B13=1 or B12=1)

    A B Result needed
    4 0
    2 1
    0 1
    3 1
    2 0
    9 0
    5 1 4
    4 0
    3 0
    2 1 3
    1 1 2
    0 1 1
    9 0
    8 0
    7 1 6
    6 1 5
    5 1 4
    4 0
    2 1 3
    0 1 2
    3 0
    2 1 1
    9 0
    0 comments No comments
  4. Ashish Mathur 101.8K Reputation points Volunteer Moderator
    2024-01-10T23:44:50+00:00

    Hi,

    In cell D2, enter this formula and drag down

    =IF(B2=0,"",IFERROR(SUM(B2:XLOOKUP(9,A2:A$25,B2:B$25)),""))

    Hope this helps.

    0 comments No comments
  5. Anonymous
    2024-01-10T23:09:44+00:00

    Hi Sola,

    Thank you for your quick reply!

    However, unfortunately the formulae didnt calculate the results i wanted. See comparison below. I think my previous example might have been confusing. Let me reiterate.

    If you look at the "results needed" column C, It should start counting the "1s" in column B, only if we see a "9" in column A, and keep counting until we see another "9" in column A, I want it to keep counting until there is another "9" and the count should start from the beginning.

    Below, you will see cell A6 is the first "9" in column A, so column C should start counting from cell C6 until C12 (counting the "1s" in column B), and the counting should continue until we see another "9" in column A cell A13, the counting should start over.

    row numbers A B C Result needed D Sola's formulae
    1 4 0 1
    2 2 1 2
    3 0 1 3
    4 3 1 3
    5 2 0 4
    6 9 1 1 5
    7 5 1 2 5
    8 4 0 5
    9 3 0 6
    10 2 1 3 7
    11 1 1 4 8
    12 0 1 5 9
    13 9 1 1 9
    14 8 0 10
    15 7 1 2 11
    16 6 1 3 12
    17 5 1 4 12
    18 4 0 13
    19 2 1 5 14
    20 0 1 6 14
    21 3 0 14
    22 2 0 15
    23 9 1 1 16
    24 5 1 2 17
    25 4 1 3 18
    26 3 1 4 18
    27 2 0 18
    28 1 0 19
    29 0 1 5 20
    30 9 1 1 20
    31 8 0 2 20

    Thank you for your help

    Sara

    0 comments No comments