match to find the 2nd occurence

rohith554

New Member
Joined
Sep 17, 2014
Messages
48
Hello All,

I need a formula that can fetch the 2nd occurrence in the row.

Below is the example data:

Name Dept

xyz 10
xhu 12
xyz 15

Now i need a formula to fetch the 2nd match(here for xyz - Answer should be 15)

xyz 15

Thanks in advance!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hey Welcome to the MessageBoard!
Put the below code in a Regular Code Module. Then you can use the function from the spreadsheet....

From the OzGrid Website:

Code:
[COLOR=#0000ff]Function[/COLOR] Nth_Occurrence(range_look[COLOR=#0000ff] As [/COLOR]Range, find_it [COLOR=#0000ff]As String[/COLOR], occurrence[COLOR=#0000ff] As Long[/COLOR], offset_row [COLOR=#0000ff]As Long[/COLOR], offset_col [COLOR=#0000ff]As Long[/COLOR])

[COLOR=#0000ff]Dim [/COLOR]lCount [COLOR=#0000ff]As Long[/COLOR]
[COLOR=#0000ff]Dim [/COLOR]rFound [COLOR=#0000ff]As Range[/COLOR]

[COLOR=#0000ff]    Set[/COLOR] rFound = range_look.Cells(1, 1)

      [COLOR=#0000ff]  For [/COLOR]lCount = 1 [COLOR=#0000ff]To[/COLOR] occurrence

            [COLOR=#0000ff]Set[/COLOR] rFound = range_look.Find(find_it, rFound, xlValues, xlWhole)[COLOR=#0000ff]
[/COLOR]
[COLOR=#0000ff]        Next[/COLOR] lCount

    Nth_Occurrence = rFound.Offset(offset_row, offset_col)

[COLOR=#0000ff]End Function[/COLOR]
 
Upvote 0
Hey Welcome to the MessageBoard!
Put the below code in a Regular Code Module. Then you can use the function from the spreadsheet....

From the OzGrid Website:

Code:
[COLOR=#0000ff]Function[/COLOR] Nth_Occurrence(range_look[COLOR=#0000ff] As [/COLOR]Range, find_it [COLOR=#0000ff]As String[/COLOR], occurrence[COLOR=#0000ff] As Long[/COLOR], offset_row [COLOR=#0000ff]As Long[/COLOR], offset_col [COLOR=#0000ff]As Long[/COLOR])

[COLOR=#0000ff]Dim [/COLOR]lCount [COLOR=#0000ff]As Long[/COLOR]
[COLOR=#0000ff]Dim [/COLOR]rFound [COLOR=#0000ff]As Range[/COLOR]

[COLOR=#0000ff]    Set[/COLOR] rFound = range_look.Cells(1, 1)

      [COLOR=#0000ff]  For [/COLOR]lCount = 1 [COLOR=#0000ff]To[/COLOR] occurrence

            [COLOR=#0000ff]Set[/COLOR] rFound = range_look.Find(find_it, rFound, xlValues, xlWhole)[COLOR=#0000ff]
[/COLOR]
[COLOR=#0000ff]        Next[/COLOR] lCount

    Nth_Occurrence = rFound.Offset(offset_row, offset_col)

[COLOR=#0000ff]End Function[/COLOR]


Thank you for the reply mrmmickle1.

I am looking for a excel formulae to get the result.

Thanks,
Rohit
 
Upvote 0
This is a formula solution:

See Below:

Excel 2012
ABCDE
1ValuesValues2xyz
2xyz101st Occ10
3xhu1st2nd Occ15
4xyz153rd Occ20
5xyz204th Occ25
6xyz25
7xhu2ndxhu
81st Occ1st
92nd Occ2nd
Sheet1
Cell Formulas
RangeFormula
E2=Nth_Occurrence($A$1:$A$7, "xyz", 1, 0, 1)
E3=Nth_Occurrence($A$1:$A$7, "xyz", 2, 0, 1)
E4=Nth_Occurrence($A$1:$A$7, "xyz", 3, 0, 1)
E5=Nth_Occurrence($A$1:$A$7, "xyz", 4, 0, 1)
E8=Nth_Occurrence($A$1:$A$7, "xhu", 1, 0, 1)
E9=Nth_Occurrence($A$1:$A$7, "xhu", 2, 0, 1)



Note: Make Sure to use the Header in your Range or the calculations will be off.

Arguments: NthOccurence(RangeToLookIn, ValueToLookFor, Occurrence, RowOffset,ColumnOffset)
 
Upvote 0
Another option.
An array formula that must be entered with
Excel Workbook
ABCDEF
1NameDept
2xyz10Find
3xhu12NameDept
4xyz15xyz15
5xyz34
6ftd56
7xhu32
8
CTRL-SHIFT-ENTER.
 
Upvote 0
Hello All,

I need a formula that can fetch the 2nd occurrence in the row.

Below is the example data:

Name Dept

xyz 10
xhu 12
xyz 15

Now i need a formula to fetch the 2nd match(here for xyz - Answer should be 15)

xyz 15

Thanks in advance!

Whichever actually suits...

1)

[TABLE="width: 192"]
<COLGROUP><COL style="WIDTH: 48pt" span=4 width=64><TBODY>[TR]
[TD="class: xl63, width: 64, bgcolor: transparent"]Name[/TD]
[TD="class: xl63, width: 64, bgcolor: transparent"]Dept[/TD]
[TD="class: xl63, width: 64, bgcolor: transparent"] [/TD]
[TD="class: xl63, width: 64, bgcolor: transparent"]xyz[/TD]
[/TR]
[TR]
[TD="class: xl63, bgcolor: transparent"]xyz[/TD]
[TD="class: xl63, bgcolor: transparent, align: right"]10[/TD]
[TD="class: xl63, bgcolor: transparent"] [/TD]
[TD="class: xl63, bgcolor: transparent, align: right"]10[/TD]
[/TR]
[TR]
[TD="class: xl63, bgcolor: transparent"]xhu[/TD]
[TD="class: xl63, bgcolor: transparent, align: right"]12[/TD]
[TD="class: xl63, bgcolor: transparent"] [/TD]
[TD="class: xl63, bgcolor: transparent, align: right"]15[/TD]
[/TR]
[TR]
[TD="class: xl63, bgcolor: transparent"]xyz[/TD]
[TD="class: xl63, bgcolor: transparent, align: right"]15[/TD]
[TD="class: xl63, bgcolor: transparent"] [/TD]
[TD="class: xl63, bgcolor: transparent"] [/TD]
[/TR]
</TBODY>[/TABLE]

D2, control+shift+enter, not just enter, anc copy down:
Rich (BB code):
=IFERROR(INDEX($B$2:$B$4,SMALL(IF($A$2:$A$4=D$1,
  ROW($A$2:$A$4)-ROW($A$2)+1),ROWS(D$2:D2))),"")

2)


[TABLE="width: 192"]
<COLGROUP><COL style="WIDTH: 48pt" span=4 width=64><TBODY>[TR]
[TD="class: xl66, width: 64, bgcolor: transparent"]Name[/TD]
[TD="class: xl66, width: 64, bgcolor: transparent"]Dept[/TD]
[TD="class: xl65, width: 64, bgcolor: transparent"] [/TD]
[TD="class: xl65, width: 64, bgcolor: transparent"]xyz[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]xyz[/TD]
[TD="class: xl65, bgcolor: transparent, align: right"]10[/TD]
[TD="class: xl65, bgcolor: transparent"] [/TD]
[TD="class: xl65, bgcolor: transparent, align: right"]2[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]xhu[/TD]
[TD="class: xl65, bgcolor: transparent, align: right"]12[/TD]
[TD="class: xl65, bgcolor: transparent"] [/TD]
[TD="class: xl66, bgcolor: transparent"]Result[/TD]
[/TR]
[TR]
[TD="class: xl65, bgcolor: transparent"]xyz[/TD]
[TD="class: xl65, bgcolor: transparent, align: right"]15[/TD]
[TD="class: xl65, bgcolor: transparent"] [/TD]
[TD="class: xl65, bgcolor: transparent, align: right"]15[/TD]
[/TR]
</TBODY>[/TABLE]

D1: xyz
D2: 2 (meaning second occurrence)

D4, control+shift+enter, not just enter:
Rich (BB code):
=IF(COUNTIF($A$2:$A$4,D$1)>=2,INDEX($B$2:$B$4,
  SMALL(IF($A$2:$A$4=D$1,ROW($A$2:$A$4)-ROW($A$2)+1),D$2)),"")
 
Upvote 0
Others have offered flexible suggestions to find the 2nd, 3rd, 4th etc occurrence but if your requirement is only to look up the 2nd occurrence, then you could use either of these non array-entered formulas.
The D2 formula should work in all Excel versions.
The D3 formula required Excel 2010 or later.

The D3 formula can also be used in a flexible way to find the nth occurrence as shown to the right of my screen shot.

Excel Workbook
ABCDEFGHIJK
1NameDeptxyzNameDeptxyz
2xyz1015xyz103
3xhu1215xhu1221
4xyz15xyz15
5abc18
6xyz21
7def24
8xyz27
9
Lookup Second
 
Upvote 0
These methods are all great. Thanks for contributing! I didn't know this was possible without a UDF
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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