Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FindNext in UDFs in Excel

Status
Not open for further replies.
Jun 3, 2005
11
SG
As has been noted in other posts, the FindNext does not work when used in a UDF (User Defined Function). This can be quite irritating / frustrating because the exact same code will work in a Sub.

However, for those wishing to have the FindNext feature available in a UDF, here is an alternative method (note: i did not think of this myself but found it on another forum; in fact it was this one: ).

Simply, this method is to use repeatedly the Find function with and employing the 'After' parameter.
Code:
Code:
Public Function FindRow(Crit As String, FindRng As Range, OldResults As Range) As Integer 
     
    Dim ans As Integer 
    Dim ans_add 
    Dim check 
    Dim c 
     
    With FindRng 
        ans_add = .Find(Crit, , lookat:=xlWhole).Address 
        ans = .Find(Crit, , lookat:=xlWhole).row 
    End With 
     
    For Each c In OldResults 
        If c.Value = ans Then 
            With FindRng 
                 'On Error GoTo ff
                ans = .Find(what:=Crit, after:=Cells(ans + 1, 1)).row 
                 'On Error GoTo 0
            End With 
        End If 
    Next 
     
ff: 
    On Error Goto 0 
    FindRow = ans 
     
End Function
again, credit goes to this guy:
 



Hi,

I would change the function data type from INTEGER to LONG since row numbers exceed 32,767, the max INTEGER value.

Skip,

[glasses] [red][/red]
[tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top