counting cells in a column if the row above has a certain value in another column

polgy

New Member
Joined
Jul 25, 2018
Messages
5
Hi everyone,

I need a formula to count the number of cells in a column only containing a certain value, if they are in the row immediately below the row containing another value in another column.

Here is an example (columns A, B, rows 1-6):

AB
1x
2y
3xy
4xy
5
6xy

<tbody>
</tbody>

I want to count how many y in B are in a row below the row of a cell containing x in A;
in the above example, this number is 2: the y on row 2 follows the x in row 1, and the y in row 4 follows the x in row 3.

Thanks!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Excel 2010
AB
1x
2y
3xy
4xy
5
6xy
7
82
Sheet10
Cell Formulas
RangeFormula
B8=SUMPRODUCT(--($A$1:$A$6="x"),--($B$2:$B$7="y"))
 
Upvote 0
Hi,

I think you can accomplish this using just COUNTIFS:


Book1
ABC
1x2
2y
3xy
4xy
5
6xy
Sheet149
Cell Formulas
RangeFormula
C1=COUNTIFS(A1:A6,"x",B2:B7,"y")
 
Upvote 0
Excel 2010
AB
1x
2y
3xy
4xy
5
6xy
7
82

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet10

Worksheet Formulas
CellFormula
B8=SUMPRODUCT(--($A$1:$A$6="x"),--($B$2:$B$7="y"))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

The double unary operator (--) converts the non numeric values x and y as 1; but the sum of the products in the columns gives 3, not 2: (1*0+0*1+1*1+1*1+1*1)=3. This formula counts the times when x and y occur on the same row. The same thing could have obtained by countifs($A$1:$A$6,"x",$B$2:$B$7,"y").
Sorry I don't need this.
 
Upvote 0
If I use this formula the result is 3, not 2.

Maybe I did not explain my problem clearly. I do not need to count the occurrences of x and y when they occur on the SAME row.

I need to count the occurrences of y when y occurs on the row BELOW the row where x occurs

See also my reply to the other suggestion to use sumproduct.
 
Upvote 0
2 things, first, I'm not understanding your response.

Second, both sheetspread and my formula works for your sample, neither formula counts the times when x and y occur on the "same" row, both formulas are "offset" by one row, have you tried them?
 
Upvote 0
Here's a larger sample.

Both my formula and sheetspread's are shown, and are producing the correct result of 4.

Look closely, if the formulas were counting x and y on the "same" row, it would have been 3.


Book1
ABCDE
1x44
2yMy formulaSheetspreads
3xy
4xy
5
6xy
7x
8y
9x
10
11y
12x
13y
14y
15x
Sheet149
Cell Formulas
RangeFormula
C1=COUNTIFS(A1:A15,"x",B2:B16,"y")
E1=SUMPRODUCT(--($A$1:$A$15="x"),--($B$2:$B$16="y"))
 
Upvote 0
@sheetspread No worries, I think OP just took a look at the formulas we provided and immediately dismissed it without Even trying them, sometimes Posters just don't realize some of the "tricks" we use, there's a lot more to an otherwise ordinary looking formula, I've had similar responses with a column offset SUMIF solution where the poster just plainly said "This is Not what I want", told me that I didn't understand what he wanted, and don't bother posting in his thread anymore...:confused: :eek:

Have a Nice Day.:)
 
Upvote 0
sorry I did not notice the two intervals are offset by one row... is this sufficient to tell excel to consistently search on row lower than in the other column... that is fabulous!
Now it seems to work: I will test the formula with other datasets but it seems it works.

For the apparent contradiction, I am unsure about what you're saying...

the y on row 2 follows the x in row 1, and the y in row 4 follows the x in row 3
refers to the calculation I need: counting y when y is on the row (row 2) below the row that contains x (row 1)

I do not need to count the occurrences of x and y when they occur on the SAME row
refers to the calculation I thought your formula did (which it doesn't, since the two intervals are offset by one row!): I thought the formula was:

=SUMPRODUCT(--($A$1:$A$6="x"),--($B$1:$B$6="y"))

instead of
=SUMPRODUCT(--($A$1:$A$6="x"),--($B$2:$B$7="y"))
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top