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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,216,225
Messages
6,129,601
Members
449,520
Latest member
TBFrieds

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