jocotterellash
New Member
- Joined
- Aug 29, 2023
- Messages
- 7
- Office Version
- 365
- Platform
- Windows
Hello,
I wonder if anyone can help me? I have a search sub function that works pretty well (finds name of a given person in a list box), however I only want it to look at my filtered data on my sheet called shstaff.
At the moment it looks at every record on shstaff even when my visible records on shstaff are filtered.
I'm fairly new to VBA with excel so learning as I go along. I would be really grateful for any advice.
Many thanks in advance!
sub findSearchResults()
Dim emplr As Variant
Dim cursearch As Variant
Dim x As Variant
Dim curRowData As Variant
emplr = shStaff.Cells.SpecialCells(xlCellTypeVisible)(Rows.Count, 1).End(xlUp).row
'clear lbx
Me.lbxResults.Clear
cursearch = Me.tbSearch
For x = 2 To emplr
curRowData = shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 1) & " " & shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 2) & " " & shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 3)
If InStr(1, curRowData, cursearch, vbTextCompare) <> 0 Then
'found it; add to listbox
Me.lbxResults.AddItem shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 1)
Me.lbxResults.List(Me.lbxResults.ListCount - 1, 1) = shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 3) & ", " & shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 2)
Me.lbxResults.List(Me.lbxResults.ListCount - 1, 2) = shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, "n")
Me.lbxResults.List(Me.lbxResults.ListCount - 1, 3) = shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, "I")
End If
Next x
End Sub
I wonder if anyone can help me? I have a search sub function that works pretty well (finds name of a given person in a list box), however I only want it to look at my filtered data on my sheet called shstaff.
At the moment it looks at every record on shstaff even when my visible records on shstaff are filtered.
I'm fairly new to VBA with excel so learning as I go along. I would be really grateful for any advice.
Many thanks in advance!
sub findSearchResults()
Dim emplr As Variant
Dim cursearch As Variant
Dim x As Variant
Dim curRowData As Variant
emplr = shStaff.Cells.SpecialCells(xlCellTypeVisible)(Rows.Count, 1).End(xlUp).row
'clear lbx
Me.lbxResults.Clear
cursearch = Me.tbSearch
For x = 2 To emplr
curRowData = shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 1) & " " & shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 2) & " " & shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 3)
If InStr(1, curRowData, cursearch, vbTextCompare) <> 0 Then
'found it; add to listbox
Me.lbxResults.AddItem shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 1)
Me.lbxResults.List(Me.lbxResults.ListCount - 1, 1) = shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 3) & ", " & shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, 2)
Me.lbxResults.List(Me.lbxResults.ListCount - 1, 2) = shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, "n")
Me.lbxResults.List(Me.lbxResults.ListCount - 1, 3) = shStaff.Cells.SpecialCells(xlCellTypeVisible)(x, "I")
End If
Next x
End Sub