Filtered Listbox run-time error 381 "Could not get the list property"

Beanie18

New Member
Joined
Feb 9, 2022
Messages
2
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I just started to use vba, so I still don't understand it very well. I made a userform with a list box to be populated by a customer database. I would like then to filtered this list box by the name of the client to be type in a text box. I found this code, but I don't get everything on it and I can only search once and then I get the error. Can someone please help me to find the error in the code bellow?

This is the line that is highlited when I click debug:

VBA Code:
TextG = ListBoxComp.List(ListBoxLines, 2)

VBA Code:
Private Sub txtCompany_Change()

    'Filter the listbox
    On Error GoTo Error
            
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    
    Dim Search As String
    Search = txtCompany
    
    Dim TotalLines As Double
    TotalLines = ListBoxComp.ListCount
    
    Dim ListBoxLines As Double
    ListBoxLines = 1
    
    Dim TextG As String
    
    If Search <> "" Then
    
    Do
    On Error Resume Next
        TextG = ListBoxComp.List(ListBoxLines, 2)
        
    If VBA.UCase(TextG) Like VBA.UCase("*" & (Search) & "*") Then
        
    Else
        
            ListBoxComp.RemoveItem (ListBoxLines)
            
            ListBoxLines = ListBoxLines - 1
            TotalLines = TotalLines - 1
    End If
        
        ListBoxLines = ListBoxLines + 1
    Loop Until ListBoxLines = TotalLines
    
    End If
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    
    Exit Sub
Error:
MsgBox "Error!", vbCritical, "Error"
        
    
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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