Nth Occurance Function

stapuff

Well-known Member
Joined
Feb 19, 2004
Messages
1,126
Below is a function that works with a formula to return the Nth occurance. It is not returning the correct data. Just appears like the function will not fail.

in sheet Champs
L1 formula is =Nth_Occurrence(N_TH,Champs!$C$3,1,0,4)
L2 formula is =Nth_Occurrence(N_TH,Champs!$C$3,2,0,4)
L3 formula is =Nth_Occurrence(N_TH,Champs!$C$3,3,0,4)
L4 formula is =Nth_Occurrence(N_TH,Champs!$C$3,4,0,4)
L5 formula is =Nth_Occurrence(N_TH,Champs!$C$3,5,0,4)
L6 formula is =Nth_Occurrence(N_TH,Champs!$C$3,6,0,4)
L7 formula is =Nth_Occurrence(N_TH,Champs!$C$3,7,0,4)

The first formula is broken down like this
N_TH is a named range of sheet1 $B$2:$B$20
Champs!$C$3 is what I want to find the number of occurances of
C3 = "CH"
1 = occurance #
0 = row.offset
4 = column.offset


Example -
In sheet1 $B$2:$B$20 there are 3 occurences of "CH"

B7 = Lima
B8 = Denver
B9 = Oakland

Here are the results I get

L1 Lima
L2 Denver
L3 Oakland
L4 Lima
L5 Denver
L6 Oakland
L7 Lima

Cells L4-L7 should fail and return an error but it just starts over



Code:
Function Nth_Occurrence(range_look As Range, find_it As String, occurrence As Long, offset_row As Long, offset_col As Long)

Dim lCount As Long
Dim rFound As Range

    Set rFound = range_look.Cells(1, 1)

        For lCount = 1 To occurrence

            Set rFound = range_look.Find(find_it, rFound, xlValues, xlWhole)

        Next lCount

    Nth_Occurrence = rFound.Offset(offset_row, offset_col)

End Function
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,

Try this fix to the UDF

Code:
Function Nth_Occurrence(range_look As Range, find_it As String, occurrence As Long, offset_row As Long, offset_col As Long)
    Dim lCount                      As Long
    Dim bottomCount                 As Long
    Dim rFound                      As Range
    Dim rFirst                      As Range
 
    'see if first cell in range contains value looking for
    'if so, set rFirst to that value, otherwise use find method
    If range_look.Cells(1, 1) = find_it Then
        Set rFirst = range_look.Cells(1, 1)
    Else
        Set rFirst = range_look.Find(find_it, range_look.Cells(1, 1), xlValues, xlWhole)
    End If
    'if rFirst is nothing, then value is nowhere in range.
    If rFirst Is Nothing Then
        Nth_Occurrence = CVErr(xlErrNA)
        Exit Function
    End If
    Set rFound = rFirst
    'counting from 2 because rFirst is the first occurrance
    For lCount = 2 To occurrence
        Set rFound = range_look.Find(find_it, rFound, xlValues, xlWhole)
 
        'if the ranges are the same, we know we "wrapped" around to the
        'beginning of the range and are now double searching
        If rFound.Address = rFirst.Address Then
            Nth_Occurrence = CVErr(xlErrNA)
            Exit Function
        End If
    Next lCount
    Nth_Occurrence = rFound.Offset(offset_row, offset_col)
End Function
 
Upvote 0
Chris -

Work very well. It returns error's where it should. Can this UDF return "" when an error occurs?

Anyways - I appreciate your suggestions.

Stapuff
 
Upvote 0
How about to using a formula?

A2, Control+Shift+Enter, not just Enter, copy down...

=INDEX($F$2:$F$20,SMALL(IF(N_TH=Champs!$C$3,ROW($F$2:$F$20)-ROW($F$2)+1),ROWS(A$2:A2)))
 
Upvote 0
StaPuff;

to have the UDF return "", just replace the lines that have
Code:
Nth_Occurrence = CVErr(xlErrNA)
with
Code:
Nth_Occurrence = ""

If this is for a production book, I'd suggest using an array formula like Haseeb provided because array formulas almost always are faster and take less memory than UDF's. (Actually, an array formula was my first thought, but I really haven't been doing enough formula work the past few months, so...)

If you're playing around with some numbers, the UDF is possibly better just because there's a lot less to type
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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