Count adjacent column after given criteria

mnnat

New Member
Joined
Apr 14, 2009
Messages
16
Hello, I'm looking for formula help. What I'm looking to do is COUNT the number of instances a value shows up in Column B, AFTER its value shows up in Column A. Consider the following data set:

Col A Col B
Jim Bob
Bob harry
Harry Jane
Bill Jane
Jim Bob
Jane Jim
Bob Jane
Harry Jim
Bill Jane
Jane Jim

1) What I'm looking for is counting the number of "Jim" in Column B AFTER the most recent occurrence of Jim in Column A (in this case, the 5th position down in Column A). The answer here should be "3" if I'm using the COUNT function.

In some form I think I need the OFFSET, MATCH and COUNTIF, but I can't wrap my arms around how to write it.

In other terms, the formula could be said as "Find the most recent occurrence of Jim in Column A, and count how many occurrences of Jim are in Column B after that point."

Thank you for the assistance.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this:

=COUNTIF(INDEX(B1:B100,MAX(IF(A1:A100="Jim",ROW(A1:A100)))):INDEX(B1:B100,MATCH("zzzzz",A1:A100)),"jim")
Array formula- confirm CTRL+SHIFT+ENTER
 
Upvote 0
Find the most recent occurrence of Jim in Column A, ...
1. Will there always be one?

2. If not, what result would you have wanted if there was no 'Jim' in column A and column B was as in your sample above?


(Robert, assuming the data is in A1:B10, change the 'Jane' in A10 to 'Jim' and observe your formula's result. It *may* be what the OP wants but with the normal meaning of 'after' I think the result should be 0)
 
Last edited:
Upvote 0
Hi Peter,
Thank you for your coments and advice.
In this case:
=COUNTIF(INDEX(B1:B100,MAX(IF(A1:A100="Jim",ROW(A1:A100)))+1):INDEX(B1:B100,MATCH("zzzzz",A1:A100)+1),"Jim")
(array formula confirm CTRL+SHIFT+ENTER)

The COUNTIF function should start to count Jims in column B one row after (or below) last "Jim" in column A.
Now the "Jane"-"Jim" combination comes to 0.
 
Upvote 0
Let A1:B11 on Sheet1 house the sample you posted...

<TABLE style="WIDTH: 96pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=128><COLGROUP><COL style="WIDTH: 48pt" span=2 width=64><TBODY><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: #95b3d7 0.5pt solid; BORDER-LEFT: #95b3d7 0.5pt solid; FONT-STYLE: italic; WIDTH: 48pt; FONT-FAMILY: Calibri; BACKGROUND: #4f81bd; HEIGHT: 14.4pt; COLOR: white; FONT-SIZE: 11pt; BORDER-TOP: #95b3d7 0.5pt solid; FONT-WEIGHT: 700; BORDER-RIGHT: #f0f0f0; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #4F81BD none" class=xl65 height=19 width=64>Field-1</TD><TD style="BORDER-BOTTOM: #95b3d7 0.5pt solid; BORDER-LEFT: #f0f0f0; FONT-STYLE: italic; WIDTH: 48pt; FONT-FAMILY: Calibri; BACKGROUND: #4f81bd; COLOR: white; FONT-SIZE: 11pt; BORDER-TOP: #95b3d7 0.5pt solid; FONT-WEIGHT: 700; BORDER-RIGHT: #95b3d7 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #4F81BD none" class=xl65 width=64>Field-2</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Verdana; BACKGROUND: #dce6f1; HEIGHT: 14.4pt; COLOR: red; FONT-SIZE: 10pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl66 height=19>Jim</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Calibri; BACKGROUND: #dce6f1; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl67>Bob</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 14.4pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68 height=19>Bob</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; FONT-FAMILY: Calibri; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none" class=xl67>harry</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Verdana; BACKGROUND: #dce6f1; HEIGHT: 14.4pt; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl68 height=19>Harry</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Calibri; BACKGROUND: #dce6f1; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl67>Jane</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 14.4pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68 height=19>Bill</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; FONT-FAMILY: Calibri; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none" class=xl67>Jane</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Verdana; BACKGROUND: #dce6f1; HEIGHT: 14.4pt; COLOR: red; FONT-SIZE: 10pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl66 height=19>Jim</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Calibri; BACKGROUND: #dce6f1; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl67>Bob</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 14.4pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68 height=19>Jane</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; FONT-FAMILY: Calibri; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none" class=xl67>Jim</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Verdana; BACKGROUND: #dce6f1; HEIGHT: 14.4pt; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl68 height=19>Bob</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Calibri; BACKGROUND: #dce6f1; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl67>Jane</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 14.4pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl68 height=19>Harry</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; FONT-FAMILY: Calibri; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none" class=xl67>Jim</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Verdana; BACKGROUND: #dce6f1; HEIGHT: 14.4pt; COLOR: black; FONT-SIZE: 10pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl68 height=19>Bill</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; FONT-FAMILY: Calibri; BACKGROUND: #dce6f1; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none; mso-pattern: #DCE6F1 none" class=xl67>Jane</TD></TR><TR style="HEIGHT: 14.4pt" height=19><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; FONT-FAMILY: Calibri; HEIGHT: 14.4pt; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none" class=xl67 height=19>Jane</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; FONT-FAMILY: Calibri; COLOR: black; FONT-SIZE: 11pt; BORDER-TOP: windowtext 0.5pt solid; FONT-WEIGHT: 400; BORDER-RIGHT: windowtext 0.5pt solid; TEXT-DECORATION: none; text-underline-style: none; text-line-through: none" class=xl67>Jim</TD></TR></TBODY></TABLE>

Define BigStr by means of Insert | Name | Define or Formulas | Name Manager
as referring to:

=REPT("z",255)

Define BigNum as referring to:

=9.99999999999999E+307

or just to:

=9.99E+307

Define Lrec as referring to:

=MATCH(BigStr,Sheet1!$B:$B)

Now we can invoke in order to "Find the most recent occurrence of Jim in Column A,
and count how many occurrences of Jim are in Column B after that point."

=COUNTIF(OFFSET(INDEX(B:B,Lrec),0,0,-(Lrec-MATCH(BigNum,IF(A2:INDEX(A:A,Lrec)="jim",1))-1)),"jim")
 
Upvote 0
Aladin

I presume that formula also needs to be array-entered, but unless I haven't implemented it correctly (I have allowed for different sheet names in defining 'Lrec') I think it fails in some circumstances. Whether these circumstances are possible with the OP's data I don't know.

Excel Workbook
ABCD
1JimBob#REF!
2Bobharry
3HarryJane
4BillJane
5JimBob
6JaneJim
7BobJane
8HarryJim
9BillJane
10JimJim
11
Ex A



Excel Workbook
ABCD
1JimBob2
2Bobharry
3HarryJane
4JimBob
5JaneJim
6BobJane
7HarryJim
8BillJane
9TomBob
10Jim
11Ken
12
Ex B




This seems to work for combinations I have tried:

Excel Workbook
ABCD
1JimBobJim
2Bobharry3
3HarryJane
4BillJane
5JimBob
6JaneJim
7BobJane
8HarryJim
9BillJane
10JaneJim
11
EX C
 
Upvote 0
Aladin

I presume that formula also needs to be array-entered, but unless I haven't implemented it correctly (I have allowed for different sheet names in defining 'Lrec') I think it fails in some circumstances. Whether these circumstances are possible with the OP's data I don't...

Yes, the formula needs to be confirmed with control+shift+enter, not just
with enter. If the last 2-item record consists of Jim and Jim, we will indeed
get a #REF! error, which would be an informative result. This was not
unintended. We can augment the formula with control expressions (thereby
making it more expesive), if such result must be avoided. By the way, Lrec
is used in order to create a dynamic named range. In Lrec we need to refer to the sheet housing the data of interest, so I have no idea what you mean by
different sheet names.
 
Upvote 0
Hello, I'm looking for formula help. What I'm looking to do is COUNT the number of instances a value shows up in Column B, AFTER its value shows up in Column A. Consider the following data set:

Col A Col B
Jim Bob
Bob harry
Harry Jane
Bill Jane
Jim Bob
Jane Jim
Bob Jane
Harry Jim
Bill Jane
Jane Jim

1) What I'm looking for is counting the number of "Jim" in Column B AFTER the most recent occurrence of Jim in Column A (in this case, the 5th position down in Column A). The answer here should be "3" if I'm using the COUNT function.

In some form I think I need the OFFSET, MATCH and COUNTIF, but I can't wrap my arms around how to write it.

In other terms, the formula could be said as "Find the most recent occurrence of Jim in Column A, and count how many occurrences of Jim are in Column B after that point."

Thank you for the assistance.
Try this...

Book1
ABCD
2JimBob_Jim
3Bobharry_3
4HarryJane__
5BillJane__
6JimBob__
7JaneJim__
8BobJane__
9HarryJim__
10BillJane__
11JaneJim__
Sheet2

D2 = count criteria

Formula entered in D3:

=COUNTIF(B11:INDEX(B:B,LOOKUP(2,1/(A2:A11=D2),ROW(A2:A11))),D2)
 
Upvote 0
In Lrec we need to refer to the sheet housing the data of interest, so I have no idea what you mean by
different sheet names.
In the examples I posted I used sheet names of 'Ex A' and 'Ex B'. I was just pointing out that I hadn't forgotten to amend the definition of Lrec to reflect those names instead of 'Sheet1'. :)
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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