ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Nth_Occurence User Defined Formula (https://www.excelbanter.com/excel-programming/346627-nth_occurence-user-defined-formula.html)

tomandrobyn

Nth_Occurence User Defined Formula
 

I've tried using the Nth_Occurence function (below) from the Ozgrid
site. The problem is when I set it up for 1,2,3,4,5 occurences and if
there is only one occurence it shows that one in all 5 cells. Could a
slight change in this code return a blank if looking for a 2nd
occurnace and there is no 2nd?
Thanks in advance,
Tom

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


--
tomandrobyn
------------------------------------------------------------------------
tomandrobyn's Profile: http://www.excelforum.com/member.php...o&userid=13879
View this thread: http://www.excelforum.com/showthread...hreadid=488265


Leith Ross[_337_]

Nth_Occurence User Defined Formula
 

Hello Tomandrobyn,

The problem is the loop doesn't detect when you have started reading
through the range again. I made som changes to detect when the loop has
start reading through the range again.

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
Dim rStart As Range

Set rFound = range_look.Cells(1, 1)
rStart = rFound

For lCount = 1 To occurrence
Set rFound = range_look.Find(find_it, rFound, xlValues, xlWhole)
if rFound.Address = rStart.Address And lCount 1 Then Exit For
Next lCount
Nth_Occurrence = rFound.offset(offset_row, offset_col)
End Function

Sincerely,
Leith Ross


--
Leith Ross
------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=488265


Dave Peterson

Nth_Occurence User Defined Formula
 
One way:

Option Explicit
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

If Application.CountIf(range_look, find_it) < occurrence Then
Nth_Occurrence = ""
Exit Function
End If

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

But I think that there's a problem if the first item in the range matches the
find_it string.

I think I'd replace:
Set rFound = range_look.Cells(1, 1)
with
Set rFound = range_look.Cells(range_look.Cells.Count)

And I like to specify all the parms in the .find command--and I like to see the
keywords, too:

Option Explicit
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

If Application.CountIf(range_look, find_it) < occurrence Then
Nth_Occurrence = ""
Exit Function
End If

'removed
'Set rFound = range_look.Cells(1, 1)

Set rFound = range_look.Cells(range_look.Cells.Count)

For lCount = 1 To occurrence
Set rFound = range_look.Find(what:=find_it, _
after:=rFound, LookIn:=xlValues, _
lookat:=xlWhole, searchorder:=xlByRows, _
searchdirection:=xlNext)
Next lCount
Nth_Occurrence = rFound.Offset(offset_row, offset_col)

End Function



tomandrobyn wrote:

I've tried using the Nth_Occurence function (below) from the Ozgrid
site. The problem is when I set it up for 1,2,3,4,5 occurences and if
there is only one occurence it shows that one in all 5 cells. Could a
slight change in this code return a blank if looking for a 2nd
occurnace and there is no 2nd?
Thanks in advance,
Tom

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

--
tomandrobyn
------------------------------------------------------------------------
tomandrobyn's Profile: http://www.excelforum.com/member.php...o&userid=13879
View this thread: http://www.excelforum.com/showthread...hreadid=488265


--

Dave Peterson

tomandrobyn[_2_]

Nth_Occurence User Defined Formula
 

Dave, thanks for the code, that works perfect!

Leith, thanks for replying but I did have a problem with the code..
the formulas turned to VALUE errors?

Thanks again guys, you're life savers

--
tomandroby
-----------------------------------------------------------------------
tomandrobyn's Profile: http://www.excelforum.com/member.php...fo&userid=1387
View this thread: http://www.excelforum.com/showthread.php?threadid=48826



All times are GMT +1. The time now is 03:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com