Listbox with search

RAires

New Member
Joined
Aug 31, 2020
Messages
21
Office Version
  1. 365
Platform
  1. Windows
hello everyone!

I currently have a form with a listbox that shows the table from the worksheet.
I want to add a search button that when you search for a value it will change the listbox to only show the rows(but display all the info from the columns for that specific item), where the value you typed is at least matched in one of the columns, kinda like a filter.
I searched for a way to do it but all i found was people working with empy listboxes and then doing the search part.

my question is how do i go about doing this?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You don't give an over abundance of information, this is one possible approach.
Sheet2 is a spare blank sheet.
VBA Code:
Private Sub UserForm_Initialize()
    Me.ListBox1.List = Sheets("Sheet1").ListObjects("Table1").DataBodyRange.Value
End Sub



Private Sub SearchButton_Click()
    Dim oLo As ListObject
    Dim i As Long, j As Long, k As Long
    
Set oLo = Sheets("Sheet1").ListObjects("Table1")

If Me.TextBox1 = vbNullString Then
    Me.ListBox1.List = oLo.DataBodyRange.Value
    Exit Sub
End If

Sheets("Sheet2").Cells.ClearContents
k = 1

With oLo
    For i = 1 To .ListRows.Count
        For j = 1 To .ListColumns.Count
            If .DataBodyRange.Cells(i, j) = Me.TextBox1 Then
                .ListRows(i).Range.Copy Sheets("Sheet2").Cells(k, 1)
                k = k + 1
                Exit For
            End If
        Next j
    Next i
End With

Me.ListBox1.List = Sheets("Sheet2").Cells(1).CurrentRegion.Value
    
End Sub
 
Upvote 0
Seeing where this thread is going I'm sure glad you never acknowledged the response to your question here.
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,159
Members
448,948
Latest member
spamiki

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