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
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Insert a textbox1 and a commandbutton1 in your userform, fill in the textbox and press the button

Put the following code in the form

Code:
Private Sub CommandButton1_Click()
    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 Lines(x) Like "*" & 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
can't it be done like as soon as I start typing, it should begin to search and when pressing backspace it should return the list?
 
Upvote 0
Yes I could, but you did not specify it in your request, it was not clear, I gladly help you make the change, but you could be very specific in what you need.
 
Upvote 0
can't it be done like as soon as I start typing, it should begin to search and when pressing backspace it should return the list?
Your ListBox and TextBox are in a UserForm, correct? If so, then I think the following code will do what you want, but note that if you have any existing event procedures that are the same as the ones below, they will have to be removed (or, any additional code integrated into them).
Code:
Public PublicLines As Variant

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)
  ReDim PublicLines(0 To UBound(Lines))
  With rList
    For x = UBound(Lines) To 0 Step -1
      PublicLines(x) = Mid(Lines(x), InStr(Lines(x), "|") + 1)
      .AddItem PublicLines(x)
    Next
  End With
End Sub

Private Sub rList_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  With TextBox1
    If KeyCode = vbKeyLeft Then
      rList.ListIndex = -1
      .SelStart = Len(.Text)
      .SetFocus
    ElseIf KeyCode = vbKeyReturn Then
      rList.ListIndex = (rList.ListIndex + 1) Mod rList.ListCount
    End If
  End With
End Sub

Private Sub TextBox1_Change()
  Dim x As Long
  rList.Clear
  For x = 0 To UBound(PublicLines)
    If InStr(1, PublicLines(x), TextBox1.Text, vbTextCompare) = 1 Then rList.AddItem PublicLines(x)
  Next
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  With rList
    If KeyCode = vbKeyReturn Then
      KeyCode = 0
      If .ListCount = 0 Then
        .Selected(0) = True
      ElseIf .ListCount = 1 Then
        .Selected(0) = True
      Else
        .SetFocus
        .Selected(0) = True
        .ListIndex = 0
      End If
    ElseIf (KeyCode = vbKeyDown Or (KeyCode = vbKeyRight And TextBox1.SelStart = Len(TextBox1.Text))) And .ListCount > 0 Then
      .SetFocus
      .Selected(0) = True
      .ListIndex = 0
    End If
  End With
End Sub

NOTES
-----------------------------------------------------
1) Not sure why you set the ListBox up as a two-column ListBox with the first column of zero width, but my code assumes a single column ListBox so make sure to change your ListBox ColumnCount property to 1.

2) You did not tell us the name of your TextBox so I assumed TextBox1 (you will have to change all of the TextBox1 references if my guess was wrong). As for the ListBox name, I used your rList name as that is what your posted code used.

3) All of the above code goes in the UserForm code module.

4) The PublicLines variable must be located outside of any procedures and at the top of the code.

5) As written, all my code does is allow the user to select an item in the ListBox, the assumption being that you will have an "action button" on the UserForm that when clicked, will do something with that selected item.

6) As you type, the ListBox list will shrink to show only those items that start with the text typed in so far. At any point you can either mouse click a showing item to select it or press the Enter Key to drop into the list... you will start with the first showing item... either press the Enter Key again or press the down arrow to advance to the next item in the list (the Enter Key will cycle back to the top of the list if you press it when the list showing item is selected). If you accidentally press the Enter Key or accidentally mouse click an item, you can either click into the TextBox or press the Left Array Key in order to resume typing in the TextBox.
 
Last edited:
Upvote 0
can't it be done like as soon as I start typing, it should begin to search and when pressing backspace it should return the list?


Try this.

Change the code for 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 Lines(x) Like "*" & 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
@DanteAmor i'm sorry, the code work as i wanted but it could not search for lines that have more than a word on them.
for instance, i have
apple
grape
pine apple

so, if i type app, i expect it to bring apple and pine apple, but it only brings apple.
 
Last edited:
Upvote 0
@DanteAmor i'm sorry, the code work as i wanted but it could not search for lines that have more than a word on them.
for instance, i have
apple
grape
pine apple

so, if i type app, i expect it to bring apple and pine apple, but it only brings apple.
Just wondering... did you try the code I posted yet?
 
Upvote 0
@DanteAmor i'm sorry, the code work as i wanted but it could not search for lines that have more than a word on them.
for instance, i have
apple
grape
pine apple

so, if i type app, i expect it to bring apple and pine apple, but it only brings apple.

That just makes my code.
Did you change something in the code?

1dc2bd3806550f9fcb6a9a7eedd1544b.jpg
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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