Search textbox for listbox

yinkajewole

Active Member
Joined
Nov 23, 2018
Messages
281
Please, I need a search textbox for this code
Code:
Private Sub UserForm_Initialize()   
Dim x As Long, FileNum As Long, TotalFile As String, Lines As Variant
FileNum = FreeFile
Open "C:\Temp\History.txt" For Binary As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL] 
TotalFile = Space(LOF(FileNum))
Get [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL] , , TotalFile
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL] 
Lines = Split(TotalFile, vbCrLf)
With rList
For x = UBound(Lines) To 0 Step -1
    .AddItem Replace(Lines(x), "|", vbNullChar, , 1)
    .List(.ListCount - 1, 1) = Mid(Lines(x), InStr(1, Lines(x), "|") + 1)
Next
End With
rList.ColumnWidths = "0;200"


End Sub
The listbox ColumnCount = -1
 
My code on the button and in the change event is the same, if it works on the button, it certainly works in the event. Check the data, it worked well in my tests.
You should try any provided code, for the benefit of the doubt.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
My code on the button and in the change event is the same, if it works on the button, it certainly works in the event. Check the data, it worked well in my tests.
You should try any provided code, for the benefit of the doubt.
I'm sorry, it worked. I don't understand why it did not work initially. Thanks.
 
Upvote 0
Now I understand what happened earlier. It was because, it is case sensitive . I don't think you can do anything to it without converting the ListBox Items to lowercase, do you?

Use this:

Code:
Private Sub TextBox1_Change()
    Dim x As Long, FileNum As Long, TotalFile As String, Lines As Variant
    FileNum = FreeFile
    Open "C:\Temp\History.txt" For Binary As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL]  
    TotalFile = Space(LOF(FileNum))
    Get [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL]  , , TotalFile
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum]#FileNum[/URL]  
    Lines = Split(TotalFile, vbCrLf)
    With rList
        .Clear
        For x = UBound(Lines) To 0 Step -1
            If [COLOR=#0000ff]ucase[/COLOR](Lines(x)) Like "*" & [COLOR=#0000ff]ucase[/COLOR](TextBox1.Value) & "*" Then
                .AddItem Replace(Lines(x), "|", vbNullChar, , 1)
                .List(.ListCount - 1, 1) = Mid(Lines(x), InStr(1, Lines(x), "|") + 1)
            End If
        Next
    End With
End Sub
 
Upvote 0
Use this:

Code:
Private Sub TextBox1_Change()
    Dim x As Long, FileNum As Long, TotalFile As String, Lines As Variant
    FileNum = FreeFile
    Open "C:\Temp\History.txt" For Binary As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL]  
    TotalFile = Space(LOF(FileNum))
    Get [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL]  , , TotalFile
    Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FileNum"]#FileNum[/URL]  
    Lines = Split(TotalFile, vbCrLf)
    With rList
        .Clear
        For x = UBound(Lines) To 0 Step -1
            If [COLOR=#0000ff]ucase[/COLOR](Lines(x)) Like "*" & [COLOR=#0000ff]ucase[/COLOR](TextBox1.Value) & "*" Then
                .AddItem Replace(Lines(x), "|", vbNullChar, , 1)
                .List(.ListCount - 1, 1) = Mid(Lines(x), InStr(1, Lines(x), "|") + 1)
            End If
        Next
    End With
End Sub

perfect!
 
Upvote 0
I have another lines of code that I would like to have a search box also for it, should I post it here or start a new thread?
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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