I have set-up a listview control on a userform, which works well when searching through a named sheet. The results that appear in the listview are perfect.
The problem with my listview, is that is shows the row number of the record, which I am trying to figure out a way of removing, but not have any success.
My code is as follows:
Private Sub CommandButton1_Click()
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:=R
For Col = 1 To 13
Set C = Wks.Cells(R, Col)
ListView1.ListItems(Cnt).ListSubItems.Add Index:=Col, 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
With ListView1
.Gridlines = True
.View = lvwReport
.HideSelection = False
.FullRowSelect = True
.HotTracking = True
.HoverSelection = False
.ColumnHeaders.Add Text:="Row", Width:=64
End With
Set Wks = ActiveWorkbook.Sheets("Live Job List")
For C = 1 To 3
ListView1.ColumnHeaders.Add Text:=Wks.Cells(4, C).Text
ComboBox1.AddItem Wks.Cells(4, C).Text
Next C
End Sub
Please can anyone see where I am going wrong?
Many thanks for taking the time to read my post and any help is much appreciated.
The problem with my listview, is that is shows the row number of the record, which I am trying to figure out a way of removing, but not have any success.
My code is as follows:
Private Sub CommandButton1_Click()
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:=R
For Col = 1 To 13
Set C = Wks.Cells(R, Col)
ListView1.ListItems(Cnt).ListSubItems.Add Index:=Col, 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
With ListView1
.Gridlines = True
.View = lvwReport
.HideSelection = False
.FullRowSelect = True
.HotTracking = True
.HoverSelection = False
.ColumnHeaders.Add Text:="Row", Width:=64
End With
Set Wks = ActiveWorkbook.Sheets("Live Job List")
For C = 1 To 3
ListView1.ColumnHeaders.Add Text:=Wks.Cells(4, C).Text
ComboBox1.AddItem Wks.Cells(4, C).Text
Next C
End Sub
Please can anyone see where I am going wrong?
Many thanks for taking the time to read my post and any help is much appreciated.