.FindNext problem

M

ManicMiner17

Sorry this is a long post but I've been working on this forever!

I have been trying to develop a function using Find/FindNext to search
for text.

The code below is just the start of the development. What it tries to do
is look in a range in a single column and identify all occurrences of a
particular text string.

I started by writing a sub which works fine and identifies all the
occurrences of the string in parameter A (code copied from Chip Pearson):

(Parameter B will be use to compare to a 2nd column, C is the range to
search)
______________________________________________________
Sub FactorX2(A As String, B As String, C As Range)
Dim FoundCell As Range
Dim LastCell As Range
Dim FirstAddress As String

With C
Set LastCell = .Cells(.Cells.Count)
End With

Set FoundCell = C.Find(what:=A, after:=LastCell, LookAt:=xlWhole)
If Not FoundCell Is Nothing Then
FirstAddress = FoundCell.Address
End If

Do Until FoundCell Is Nothing
Set FoundCell = C.FindNext(after:=FoundCell)
Debug.Print FoundCell.Address
If FoundCell.Address = FirstAddress Then
Exit Do
End If
Loop
End Sub
_______________________________________________________

I then tried to convert this to a function:

Function FactorX(A As String, B As String, C As Range) As Variant
Dim FoundCell As Range
Dim LastCell As Range
Dim FirstAddress As String

With C
Set LastCell = .Cells(.Cells.Count)
End With

Set FoundCell = C.Find(what:=A, after:=LastCell, LookAt:=xlWhole)
If Not FoundCell Is Nothing Then
FirstAddress = FoundCell.Address
End If

Do Until FoundCell Is Nothing
Set FoundCell = C.FindNext(after:=FoundCell)
Debug.Print "£$ " & FoundCell.Address
If FoundCell.Address = FirstAddress Then
Exit Do
End If
Loop
FactorX = 0
End Function
__________________________________________________________

The function finds the first string occurrence, but the
FindNext fails completely. Even if I place Debug.Print Statements before
End Function, nothing is printed. It is as though the
code exits as soon as it hits the FindNext.

Can anyone please shed any light on this?
 
M

ManicMiner17

Sorry this is a long post but I've been working on this forever!

I have been trying to develop a function using Find/FindNext to search
for text.

I should have said I'm trying to create a function called from a worksheet.

I found this on Chip Pearson's site:

"If you attempt to change anything, the function will terminate
immediately and return a #VALUE error to the calling cell. In Excel 97
and 2000, a UDF cannot use the Find method of a Range object, even
though that method does not change anything in Excel. This was fixed
with Excel 2002."

Looks like it got broken again in 2003!
 
M

MK

I've run into a similar problem, where FindNext does not find subsequent matches. Have you found a solution to yours?
 
M

MK

I've run into a similar problem, where FindNext does not find subsequent matches. Have you found a solution to yours?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top