Custom Function: Nth Occurrence, not returning a result

Lizabeta

New Member
Joined
Oct 24, 2008
Messages
32
I had some success with this in another workbook. Though I was having a problem. I started making a 'generic' workbook for here, added the custom function and updated my formulas. Now it won't return any values. It just gives me 0:00 instead of returning the time.

My original problem was that instead of returning only a 2nd occurrence if there was one... if there was NO 2nd occurrence, it would return the 1st occurrence.

Code:
Function Nth(find_it As String, range_look As Range, 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

and my formula:
=Nth($A1,'Routing Times'!$A$1:$C$10,2,0,2)

=Nth(find_it,range_look,occurrence,offset_row,offset_col)

So it should be:
=Nth(Jane Doe, on the routing sheet range, 2nd occurrence, no offset row, 2nd column from reference)

This is returning 0:00 in my 'generic test' workbook when it should return the time listed.
In my original workbook, it's returning the 2nd occurrence if there is one, but otherwise the 1st occurrence. I only want it to return a 2nd occurrence if there is one, otherwise nothing.

I'm not seeing a place to attach my 'generic' workbook... if someone could point out how, I'd be happy to attach my bitty bitty workbook.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Can you work with something like this?:
Code:
Function Nth(find_it As String, range_look As Range, occurrence As Long, offset_row As Long, offset_col As Long)
Dim lCount As Long
Dim cCell As Range
lCount = 0
On Error GoTo errTrap
For Each cCell In range_look.Resize(ColumnSize:=1)
    If cCell.Value = find_it Then
        lCount = lCount + 1
        If lCount = occurrence Then
            Nth = cCell.Offset(RowOffset:=offset_row, ColumnOffset:=offset_col)
            Exit For
        End If
    End If
Next cCell
errTrap:
If lCount <> occurrence Then
    Nth = "No match"
End If
End Function
 
Upvote 0
Its giving me a "No Match" for one I know has several matches... Let me play with it a bit and see if I can get it to work.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,282
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