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):

[TABLE="width: 200"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]x[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]y[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]x[/TD]
[TD]y[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]x[/TD]
[TD]y[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]x[/TD]
[TD]y[/TD]
[/TR]
</tbody>[/TABLE]

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 can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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
x
y
xy
xy
xy

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]6[/TD]

[TD="align: center"]7[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]8[/TD]
[TD="align: right"][/TD]
[TD="align: right"]2[/TD]

</tbody>
Sheet10

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]B8[/TH]
[TD="align: left"]=SUMPRODUCT(--($A$1:$A$6="x"),--($B$2:$B$7="y"))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

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: :eeek:

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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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