Filtering ListBox Based On TextBox Values In UserForm

Orpheus600

New Member
Joined
Mar 25, 2015
Messages
6
Hello Again Guys,

Another stumper I have come across, I am trying to filter my list box using a TextBox but despite using many solutions previously posted here I cannot get them to work specifically one line of the code keeps producing Error but it is the key to the code I have highlighted the error line. Driver_Works_List is my ListBox, Filter_Depot_TextBox is my TextBox
Code:
Sub Lookup_Depot()
'declare the variables
    Dim rngFind As Range
    Dim strFirstFind As String
    'clear the listbox
    [COLOR="#FF0000"]Driver_Works_Form.Driver_Works_List.Clear[/COLOR] 
    With Sheets("Driver Work Orders").Range("B:B")
        Set rngFind = .Find(Driver_Works_Form.Filter_Depot_TextBox.Text, LookIn:=xlValues, lookat:=xlPart)
        'if value found then set a variable for the address
        If Not rngFind Is Nothing Then
            strFirstFind = rngFind.Address
            'add the values to the listbox
            Do
                If rngFind.Row > 1 Then
                    Driver_Works_List.AddItem rngFind.Value
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 1) = rngFind.Offset(0, 1)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 2) = rngFind.Offset(0, 2)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 3) = rngFind.Offset(0, 3)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 4) = rngFind.Offset(0, 4)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 5)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 6)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 7)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 8)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 9)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 10)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 11)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 12)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 13)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 14)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 15)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 16)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 17)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 18)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 19)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 20)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 21)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 22)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 23)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 24)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 25)
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, 26)
                End If
                'find the next address to add
                Set rngFind = .FindNext(rngFind)
            Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
        End If
    End With
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hello
This worked for me:

Code:
Sub Lookup_Depot()
Dim rngFind As Range, strFirstFind$, i%


Driver_Works_Form.Driver_Works_List.RowSource = ""  ' unlink control from sheet


Driver_Works_Form.Driver_Works_List.Clear
Driver_Works_Form.Driver_Works_List.ColumnCount = 5
    
With Sheets("Driver Work Orders").Range("B:B")
    Set rngFind = .Find(Driver_Works_Form.Filter_Depot_TextBox.Text, LookIn:=xlValues, lookat:=xlPart)
    'if value found then set a variable for the address
    If Not rngFind Is Nothing Then
        strFirstFind = rngFind.Address
        'add the values to the listbox
        Do
            If rngFind.Row > 1 Then
                Driver_Works_List.AddItem rngFind.Value
                Driver_Works_List.List(Driver_Works_List.ListCount - 1, 1) = rngFind.Offset(0, 1)
                Driver_Works_List.List(Driver_Works_List.ListCount - 1, 2) = rngFind.Offset(0, 2)
                Driver_Works_List.List(Driver_Works_List.ListCount - 1, 3) = rngFind.Offset(0, 3)
                Driver_Works_List.List(Driver_Works_List.ListCount - 1, 4) = rngFind.Offset(0, 4)
                For i = 5 To 26
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, i)
                Next
            End If
            'find the next address to add
            Set rngFind = .FindNext(rngFind)
        Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
    End If
End With
End Sub
 
Upvote 0
Hello Worf,

Thank you for your reply, I have entered the code you specified which got me past the List Clear error. but now it is giving me an Object not Defined error at the line highlighted below.
Hello
This worked for me:

Code:
Sub Lookup_Depot()
Dim rngFind As Range, strFirstFind$, i%


Driver_Works_Form.Driver_Works_List.RowSource = ""  ' unlink control from sheet


Driver_Works_Form.Driver_Works_List.Clear
Driver_Works_Form.Driver_Works_List.ColumnCount = 5
    
With Sheets("Driver Work Orders").Range("B:B")
    Set rngFind = .Find(Driver_Works_Form.Filter_Depot_TextBox.Text, LookIn:=xlValues, lookat:=xlPart)
    'if value found then set a variable for the address
    If Not rngFind Is Nothing Then
        strFirstFind = rngFind.Address
        'add the values to the listbox
        Do
            If rngFind.Row > 1 Then
                [COLOR="#FF0000"]Driver_Works_List.AddItem rngFind.Value[/COLOR]
                Driver_Works_List.List(Driver_Works_List.ListCount - 1, 1) = rngFind.Offset(0, 1)
                Driver_Works_List.List(Driver_Works_List.ListCount - 1, 2) = rngFind.Offset(0, 2)
                Driver_Works_List.List(Driver_Works_List.ListCount - 1, 3) = rngFind.Offset(0, 3)
                Driver_Works_List.List(Driver_Works_List.ListCount - 1, 4) = rngFind.Offset(0, 4)
                For i = 5 To 26
                    Driver_Works_List.List(Driver_Works_List.ListCount - 1, 5) = rngFind.Offset(0, i)
                Next
            End If
            'find the next address to add
            Set rngFind = .FindNext(rngFind)
        Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
    End If
End With
End Sub
 
Upvote 0
Is it a compile or execution error? What is the error number? Is it #1004?
 
Upvote 0
Please test this:

Code:
' form module / tested with Excel 2013
Sub Lookup_Depot()
Dim rngFind As Range, strFirstFind$, i%, L
Set L = Me.Driver_Works_List
L.RowSource = ""  ' unlink control from sheet
L.Clear
L.ColumnCount = 5
With Sheets("Driver Work Orders").Range("B:B")
    Set rngFind = .Find(Me.Filter_Depot_TextBox.Text, LookIn:=xlValues, lookat:=xlPart)
    'if value found then set a variable for the address
    If Not rngFind Is Nothing Then
        strFirstFind = rngFind.Address
        'add the values to the listbox
        Do
            If rngFind.Row > 1 Then
                L.AddItem rngFind.Value
                L.List(L.ListCount - 1, 1) = rngFind.Offset(0, 1)
                L.List(L.ListCount - 1, 2) = rngFind.Offset(0, 2)
                L.List(L.ListCount - 1, 3) = rngFind.Offset(0, 3)
                L.List(L.ListCount - 1, 4) = rngFind.Offset(0, 4)
                For i = 5 To 26
                    L.List(L.ListCount - 1, 5) = rngFind.Offset(0, i)
                Next
            End If
            'find the next address to add
            Set rngFind = .FindNext(rngFind)
        Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
    End If
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top