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:
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