RockandGrohl
Well-known Member
- Joined
- Aug 1, 2018
- Messages
- 801
- Office Version
- 365
- Platform
- Windows
Hi guys
I have a userform that has a search box. If the user searches "Apple" it filters a table on "IntF" sheet column C to have anything with "Apple" in it
I would then like a maximum of 20 of these to be added to my IntListBox Listbox on my userform.
I've set my Listbox to have 5 columns in Userform Initialize, and would like the filtered results for each row from cells D to H to be added to the ListBox.
Here's me code so far
In the future, I want the user to click only one of the results in the listbox and have that selection applied to ComboBoxes on the same userform. That shouldn't be too difficult.
Thank you!
I have a userform that has a search box. If the user searches "Apple" it filters a table on "IntF" sheet column C to have anything with "Apple" in it
I would then like a maximum of 20 of these to be added to my IntListBox Listbox on my userform.
I've set my Listbox to have 5 columns in Userform Initialize, and would like the filtered results for each row from cells D to H to be added to the ListBox.
Here's me code so far
VBA Code:
Private Sub IntSearchButton_Click()
Application.ScreenUpdating = False
Dim AssetVal As String
Dim intF As Worksheet
Dim Lastrow As Long
Dim filterRange As Range
Dim visibleCells As Range
Set intF = Worksheets("InterventionFilter")
Set filterRange = intF.Range("D:H")
AssetVal = Me.IntSearchBox.Value
If AssetVal = "" Then
Exit Sub
End If
On Error Resume Next
intF.ShowAllData
On Error GoTo 0
intF.Activate
Lastrow = intF.Cells(Rows.Count, "A").End(xlUp).Row
intF.Range("A1:W" & Lastrow).AutoFilter Field:=3, Criteria1:="*" & AssetVal & "*"
' Count visible cells in the filtered range
On Error Resume Next
Set visibleCells = filterRange.SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(filterRange.Rows.Count - 1)
On Error GoTo 0
If Not visibleCells Is Nothing Then
Me.IntListBox.List = visibleCells.Value
End If
' Check if there are visible cells in the filtered range
If Not visibleCells Is Nothing Then
' Your code to populate ListBox here
Me.IntListBox.List = visibleCells.Value
Else
MsgBox "No results found"
End If
Application.ScreenUpdating = True
End sub
In the future, I want the user to click only one of the results in the listbox and have that selection applied to ComboBoxes on the same userform. That shouldn't be too difficult.
Thank you!