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?
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?