Hi Sparky,
The following code should help you get started. See the attached sample workbook.
Sub FindAllMatches()
Dim r As Range
Dim iCount As Long
Dim iListBoxRow As Long
Dim iRow As Long
Dim bNeedMore As Boolean
Dim sAddress As String
Dim sFirstAddress As String
Dim sSearchSpecification As String
If Len(sSearchSpecification) = 0 Then
sSearchSpecification = Trim(Mat_Search.NmbSearch.Value)
End If
If Len(sSearchSpecification) = 0 Then
sSearchSpecification = Trim(Mat_Search.DescSearch.Value)
End If
If Len(sSearchSpecification) = 0 Then
sSearchSpecification = Trim(Mat_Search.TextBox3.Value)
End If
If Len(sSearchSpecification) > 0 Then
Mat_Search.ListBox1.Clear
iListBoxRow = -1
'Find the first occurence of the string
Set r = Nothing
Set r = Worksheets("Materials").Range("A:A,C:C,E:E").Find(What:=sSearchSpecification, _
After:=Worksheets("Materials").Range("A1"), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not r Is Nothing Then
'Save the found address as the 'First Address'
'Save the value to be returned
iCount = iCount + 1
sFirstAddress = r.Address(False, False)
Debug.Print "First occurrence of '" & sSearchSpecification & "' is in cell '" & sFirstAddress & "'."
'Search for additional values
'If found add them to the array to be returned
bNeedMore = True
While bNeedMore
iRow = r.Row
iListBoxRow = iListBoxRow + 1
Mat_Search.ListBox1.AddItem
Mat_Search.ListBox1.List(iListBoxRow, 0) = Trim(Worksheets("Materials").Cells(iRow, "A").Value)
Mat_Search.ListBox1.List(iListBoxRow, 1) = Trim(Worksheets("Materials").Cells(iRow, "C").Value)
Mat_Search.ListBox1.List(iListBoxRow, 2) = Trim(Worksheets("Materials").Cells(iRow, "E").Value)
Set r = Worksheets("Materials").Range("A:A,C:C,E:E").FindNext(After:=r)
sAddress = r.Address(False, False)
If sAddress = sFirstAddress Then
bNeedMore = False
Else
iCount = iCount + 1
Debug.Print "Next occurrence of '" & sSearchSpecification & "' is in cell '" & sAddress & "'."
End If
Wend
End If
End If
End Sub
Lewis
Bookmarks