n-th match in array without array: index(,match+n-1,)?

regresss

Board Regular
Joined
May 20, 2015
Messages
68
Hi, I have read online that one can use array formula to return nth match in array using index match. There is also some other, rather convoluted non-array formula solution.

I wonder, why one cannot not increment match value by n-1? That is assuming I have sorted my table and matches are on consecutive rows (in consecutive columns)?


A1:
Bob
John
John
Bob

B1:
1
2
3
4


Why does it not work to return the 2nd John value with INDEX(1:4,MATCH("John",A:A,0)+1,2)? Would you know of any alternative?



Edit: found out it actually works :-). Great!
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
INDEX is based in RANGE, row, column, so the problem if not with INDEX

MATCH will always only return the 1st match it finds - then stop looking.
The way you have MATCH structured above, it will find the row John is in, then go down 1 more row. So yes, you can do the n+1 thing to get consecutive rows, but if, for instance, you have 3 Johns and then a Mike, and use +4, you will get Mike, not John.

2 ways I can see to get around this

1, use a helper column to sequence-count "john"
=countif($A$1:a1,A1) copied down will give each name a "count" value.
You could then combine that with the name to get a unique ID
=A1&" "&countif($A$1:a1,A1)
To find the n-th John, you would use =MATCH("John "&"n,helper-column,;) to find what you need

Another way might be to do a sequential count for John, and use that to add it to the MATCH
 
Upvote 0
Another way might be to do a sequential count for John, and use that to add it to the MATCH

Could you elaborate on this? Helper columns are a big no no.

I also wonder if i could find the last observation of John in some simple way, assuming values cannot be sorted.
 
Last edited:
Upvote 0
I don't see why helpers are a "big no"?? They can simplify things, an can be hidden

This would use a helper, it works on unsorted data...
[Table="width:, class:grid"][tr][td] [/td][td]
A​
[/td][td]
B​
[/td][td]
C​
[/td][td]
D​
[/td][td]
E​
[/td][td]
F​
[/td][/tr]
[tr][td]
1​
[/td][td]Name[/td][td]value[/td][td]Helper[/td][td][/td][td]Name[/td][td]value[/td][/tr]

[tr][td]
2​
[/td][td]aa[/td][td]
10​
[/td][td]aa 1[/td][td][/td][td]bb[/td][td]
50​
[/td][/tr]

[tr][td]
3​
[/td][td]bb[/td][td]
20​
[/td][td]bb 1[/td][td][/td][td]
2​
[/td][td][/td][/tr]

[tr][td]
4​
[/td][td]cc[/td][td]
30​
[/td][td]cc 1[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
5​
[/td][td]aa[/td][td]
40​
[/td][td]aa 2[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
6​
[/td][td]bb[/td][td]
50​
[/td][td]bb 2[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
7​
[/td][td]cc[/td][td]
60​
[/td][td]cc 2[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
8​
[/td][td]aa[/td][td]
70​
[/td][td]aa 3[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
9​
[/td][td]bb[/td][td]
80​
[/td][td]bb 3[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
10​
[/td][td]cc[/td][td]
90​
[/td][td]cc 3[/td][td][/td][td][/td][td][/td][/tr]
[/table]

C2=A2&" "&COUNTIF($A$2:A2,A2)
copied down
F2=INDEX(B:B,MATCH(E2&" "&E3,C:C,0))

For SORTED data, this should work (which you pretty much already had)...
[Table="width:, class:grid"][tr][td] [/td][td]
H​
[/td][td]
I​
[/td][td]
J​
[/td][td]
K​
[/td][td]
L​
[/td][/tr]
[tr][td]
1​
[/td][td]Name[/td][td]value[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
2​
[/td][td]aa[/td][td]
10​
[/td][td][/td][td]Name[/td][td]value[/td][/tr]

[tr][td]
3​
[/td][td]aa[/td][td]
20​
[/td][td][/td][td]bb[/td][td]
50​
[/td][/tr]

[tr][td]
4​
[/td][td]aa[/td][td]
30​
[/td][td][/td][td]
2​
[/td][td][/td][/tr]

[tr][td]
5​
[/td][td]bb[/td][td]
40​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
6​
[/td][td]bb[/td][td]
50​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
7​
[/td][td]bb[/td][td]
60​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
8​
[/td][td]cc[/td][td]
70​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
9​
[/td][td]cc[/td][td]
80​
[/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
10​
[/td][td]cc[/td][td]
90​
[/td][td][/td][td][/td][td][/td][/tr]
[/table]

L3=INDEX(I:I,MATCH(K3,H:H,0)+K4-1)

I don't know of any other (simple) non-array way to do this
 
Upvote 0
Here are 2 ways to extract the nth match directly.


Book1
ABCDEFGH
1NamevalueNameMatchvaluevalue
2aa10bb25050
3bb20aa37070
4cc30cc13030
5aa40
6bb50
7cc60
8aa70
9bb80
10cc90
nth match
Cell Formulas
RangeFormula
G2=INDEX(B$2:B$10,AGGREGATE(15,6,(ROW(B$2:B$10)-ROW(B$2)+1)/(A$2:A$10=E2),F2))
H2{=INDEX(B$2:B$10,SMALL(IF(A$2:A$10=E2,ROW(B$2:B$10)-ROW(B$2)+1),F2))}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Pete, I considered using (at least) INDEX/SMALL/IF, but OP seemed to indicate they wanted a non-array method :)
 
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