Find the nth Value in an Index(Match list

jamesuk

Board Regular
Joined
Sep 8, 2015
Messages
85
Hi All

hope you're all well?

Is it possible to have an index(match look at and retrieve the value of the nth occurrence in an array?

For example. if in Column A I have:

Cat
Dog
Cat
Monkey
Elephant

then in column B I have:

1
3
5
6
8

I want my index match to return the value of the 2nd occurence of Cat (so, 5), how could I do this please?

Code:
  =INDEX(A1:B5,MATCH("Cat",A1:A5,0),2)
would return 1


Many thanks in advance for your help! :)
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
try this

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Array Formulas[TABLE="width: 100%"]
<tbody>[TR="bgcolor: #DAE7F5"]
[TH]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
[TR]
[TH="bgcolor: #DAE7F5"]E1[/TH]
[TD="align: left"]{=INDEX($B$1:$B$5,SMALL(IF($A$1:$A$5="Cat",ROW($A$1:$A$5),""),2),1)}[/TD]
[/TR]
</tbody>[/TABLE]
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
If using Excel 2010 or later an option without the need for Ctrl+Shift+Enter

Excel Workbook
ABCDE
1Cat1ValueCat
2Dog3Occurence2
3Cat5Result5
4Monkey6
5Elephant8
6
Match Nth
 
Upvote 0
Hi AlanY

Thanks - I tried this and it does work which is brilliant!

If there was a 3rd Cat value, and I wanted that... would I just change the number '2' in the formula to 3?
 
Upvote 0
Hi AlanY

Thanks - I tried this and it does work which is brilliant!

If there was a 3rd Cat value, and I wanted that... would I just change the number '2' in the formula to 3?

yes, exactly
 
Upvote 0
Hi Both

Thank you both for your help :) they both worked great.

But I decided to go with Peter's suggestion (as i'm using 2010)

edited a little as I realised I needed it to take the value in the cell below the occurrence in the same column :)

Code:
=INDEX(A:A,(AGGREGATE(15,6,(ROW(A:A)-ROW(A1)+1)/(A:A="Cat"),COUNTIF(A:A,"Cat"))+1))

Thank you again for your help! :)
 
Upvote 0
... (as i'm using 2010)

edited a little as I realised I needed it to take the value in the cell below the occurrence in the same column :)

Code:
=INDEX(A:A,(AGGREGATE(15,6,(ROW(A:A)-ROW(A1)+1)/(A:A="Cat"),COUNTIF(A:A,"Cat"))+1))
Especially since you are using a recent version of Excel, it is best not to use whole column references if you don't need to. Why check over 1,000,000 rows if you are using, say, only a few hundred?

Also, it appears from your formula that you are not trying to find the Nth occurrence of 'Cat' but rather the last occurrence. In that case you can change the first AGGREGATE argument to 14 (LARGE) & then you don't need the COUNTIF.

For example,
Code:
=INDEX(A1:A1000,(AGGREGATE(14,6,(ROW(A1:A1000)-ROW(A1)+1)/(A1:A1000="Cat"),1)+1))
You can change the 1000 rows if you need to but this calculates over 100 times faster than the formula you posted. :)
 
Upvote 0
Hi Peter

Quite right!

believe it or not the range of data i'm looking at can go over 500,000 rows

Im sure i'll put a specific array in when I'm finished making what i'm making, so i'll keep that in mind :)
 
Upvote 0
How could I amend to use this for numeric values.
In what way does it not already work for numbers as is?
Are you talking about the original question of finding the Nth occurrence, or the subsequent part about finding the last occurrence, or even finding the value below the last occurrence like in posts #6 & 7?
What are your particular circumstances?
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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