I have created a userform that searches specific columns on my worksheet which works perfectly.
I am now trying to create a combined search that will search all of the columns and produce the results in my listview.
My code so far is as follows:
Private Sub CommandButton1_Click()
'SEARCH
Dim Cnt As Long
Dim Col As Variant
Dim FirstAddx As String
Dim FoundMatch As Range
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Dim Wks As Worksheet
StartRow = 4
Set Wks = ActiveWorkbook.Sheets("Live Job List")
Col = ComboBox1.ListIndex + 1
If Col = 0 Then
MsgBox "Please choose a category."
Exit Sub
End If
If TextBox1.Text = "" Then
MsgBox "Please enter a search term."
TextBox1.SetFocus
Exit Sub
End If
LastRow = Wks.Cells(Rows.Count, Col).End(xlUp).Row
LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
Set Rng = Wks.Range(Wks.Cells(1, Col), Wks.Cells(LastRow, Col))
Set FoundMatch = Rng.Find(What:=TextBox1.Text, _
After:=Rng.Cells(1, 1), _
LookAt:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not FoundMatch Is Nothing Then
FirstAddx = FoundMatch.Address
ListView1.ListItems.Clear
Do
Cnt = Cnt + 1
R = FoundMatch.Row
ListView1.ListItems.Add Index:=Cnt, Text:=Wks.Cells(R, 1)
For Col = 2 To 3
Set C = Wks.Cells(R, Col)
ListView1.ListItems(Cnt).ListSubItems.Add Index:=Col - 1, Text:=C.Text
Next Col
Set FoundMatch = Rng.FindNext(FoundMatch)
Loop While FoundMatch.Address <> FirstAddx And Not FoundMatch Is Nothing
SearchRecords = Cnt
Else
ListView1.ListItems.Clear
SearchRecords = 0
MsgBox "No match found for " & TextBox1.Text
End If
End Sub
-----------------------------------------------------------------------
Private Sub UserForm_Activate()
Dim C As Long
Dim I As Long
Dim R As Long
Dim Wks As Worksheet
ComboBox1.Value = "Job"
With ListView1
.Gridlines = True
.View = lvwReport
.HideSelection = False
.FullRowSelect = True
.HotTracking = True
.HoverSelection = False
End With
Set Wks = ActiveWorkbook.Sheets("Live Job List")
For C = 1 To 3
ListView1.ColumnHeaders.Add Text:=Wks.Cells(4, C).Text, Width:=126
ComboBox1.AddItem Wks.Cells(4, C).Text
Next C
End Sub
Please can anyone assist me, as I am completely stuck!
I can email my workbook if this helps.
Many thanks for taking the time to read my post.
I am now trying to create a combined search that will search all of the columns and produce the results in my listview.
My code so far is as follows:
Private Sub CommandButton1_Click()
'SEARCH
Dim Cnt As Long
Dim Col As Variant
Dim FirstAddx As String
Dim FoundMatch As Range
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Dim Wks As Worksheet
StartRow = 4
Set Wks = ActiveWorkbook.Sheets("Live Job List")
Col = ComboBox1.ListIndex + 1
If Col = 0 Then
MsgBox "Please choose a category."
Exit Sub
End If
If TextBox1.Text = "" Then
MsgBox "Please enter a search term."
TextBox1.SetFocus
Exit Sub
End If
LastRow = Wks.Cells(Rows.Count, Col).End(xlUp).Row
LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
Set Rng = Wks.Range(Wks.Cells(1, Col), Wks.Cells(LastRow, Col))
Set FoundMatch = Rng.Find(What:=TextBox1.Text, _
After:=Rng.Cells(1, 1), _
LookAt:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not FoundMatch Is Nothing Then
FirstAddx = FoundMatch.Address
ListView1.ListItems.Clear
Do
Cnt = Cnt + 1
R = FoundMatch.Row
ListView1.ListItems.Add Index:=Cnt, Text:=Wks.Cells(R, 1)
For Col = 2 To 3
Set C = Wks.Cells(R, Col)
ListView1.ListItems(Cnt).ListSubItems.Add Index:=Col - 1, Text:=C.Text
Next Col
Set FoundMatch = Rng.FindNext(FoundMatch)
Loop While FoundMatch.Address <> FirstAddx And Not FoundMatch Is Nothing
SearchRecords = Cnt
Else
ListView1.ListItems.Clear
SearchRecords = 0
MsgBox "No match found for " & TextBox1.Text
End If
End Sub
-----------------------------------------------------------------------
Private Sub UserForm_Activate()
Dim C As Long
Dim I As Long
Dim R As Long
Dim Wks As Worksheet
ComboBox1.Value = "Job"
With ListView1
.Gridlines = True
.View = lvwReport
.HideSelection = False
.FullRowSelect = True
.HotTracking = True
.HoverSelection = False
End With
Set Wks = ActiveWorkbook.Sheets("Live Job List")
For C = 1 To 3
ListView1.ColumnHeaders.Add Text:=Wks.Cells(4, C).Text, Width:=126
ComboBox1.AddItem Wks.Cells(4, C).Text
Next C
End Sub
Please can anyone assist me, as I am completely stuck!
I can email my workbook if this helps.
Many thanks for taking the time to read my post.