Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help text for user defined formula WJvR Excel Worksheet Functions 2 July 23rd 08 04:34 PM
Formula as User Defined Function EstherJ Excel Programming 3 October 8th 04 01:09 PM
User-defined data type; Error: Only User-defined types... tiger_PRM Excel Programming 1 July 18th 04 03:32 PM
external range in VBA (user defined formula) BrianB Excel Programming 2 July 23rd 03 06:25 PM
external range in VBA (user defined formula) Tim Zych[_2_] Excel Programming 0 July 22nd 03 02:02 AM


All times are GMT +1. The time now is 05:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"