Multi column listbox and textbox search

Anthony86

Board Regular
Joined
Jan 31, 2018
Messages
57
Hi Guys,

I've got a user form with a listbox and textbox.
What I'm trying to achieve is when I type things in the textbox it searches the listbox and removes anything that doesnt relate.("*" Wildcard "*") The column I want it to search in is column C and it currently has 6 columns. What is the fastest way to achieve this without taking to much of a hit on processing?
 

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Waimea

Active Member
Joined
Jun 30, 2018
Messages
360
Hi,

how are you populating the listbox?

Are you using column headers?
 

Anthony86

Board Regular
Joined
Jan 31, 2018
Messages
57
Hi, I'm not using column headers as I've designed labels as the headers.
Code:
Private Sub UserForm_Initialize()ListBox1.ColumnCount = 6
ListBox1.ColumnWidths = "80,80,245,80,78"
ListBox1.RowSource = "DataList"


End Sub
 

Anthony86

Board Regular
Joined
Jan 31, 2018
Messages
57
I was using this snippet of code to search on a smaller database, but now I have a really big database the code isn't working.

Code:
Dim X, i As Long, ii As Long, iii As Integer    X = [DataList]
    
    With ListBox1
        If TextBox1 = "" Then
            .RowSource = "DataList"
        Else
            .RowSource = ""
            For i = 1 To UBound(X, 1)
                If LCase(X(i, 3)) Like LCase(TextBox1) & "*" Then
                    For ii = 1 To 6
                        .AddItem
                        .List(iii, ii - 1) = X(i, ii)
                    Next
                    iii = iii + 1
                End If
            Next
        End If
    End With
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,192
Office Version
2007
Platform
Windows
Try this
For a base of 20,000 records it takes less than a second for every letter you enter in the textbox.

Code:
Private Sub TextBox1_Change()
  Dim a, sh As Worksheet, i As Long, j As Long
  ListBox1.Clear
  Set sh = Sheets("Sheet1")
  a = sh.Range("A2:F" & sh.Range("C" & Rows.Count).End(xlUp).Row)
  j = 1
  ReDim b(1 To UBound(a), 1 To 6)
  For i = 1 To UBound(a)
    If a(i, 3) Like "*" & TextBox1.Value & "*" Then
      b(j, 1) = a(i, 1)
      b(j, 2) = a(i, 2)
      b(j, 3) = a(i, 3)
      b(j, 4) = a(i, 4)
      b(j, 5) = a(i, 5)
      b(j, 6) = a(i, 6)
      j = j + 1
    End If
  Next
  ListBox1.List = b()
End Sub
 

Anthony86

Board Regular
Joined
Jan 31, 2018
Messages
57
Try this
For a base of 20,000 records it takes less than a second for every letter you enter in the textbox.

Code:
Private Sub TextBox1_Change()
  Dim a, sh As Worksheet, i As Long, j As Long
  ListBox1.Clear
  Set sh = Sheets("Sheet1")
  a = sh.Range("A2:F" & sh.Range("C" & Rows.Count).End(xlUp).Row)
  j = 1
  ReDim b(1 To UBound(a), 1 To 6)
  For i = 1 To UBound(a)
    If a(i, 3) Like "*" & TextBox1.Value & "*" Then
      b(j, 1) = a(i, 1)
      b(j, 2) = a(i, 2)
      b(j, 3) = a(i, 3)
      b(j, 4) = a(i, 4)
      b(j, 5) = a(i, 5)
      b(j, 6) = a(i, 6)
      j = j + 1
    End If
  Next
  ListBox1.List = b()
End Sub
This works great, and is really fast. Thank you
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,192
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,089,395
Messages
5,408,000
Members
403,176
Latest member
mehtavish1

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top