Solved

Excel VBA: Real-Time Search or Filtering in Listbox

Posted on 2016-07-28
5
117 Views
Last Modified: 2016-08-27
I have a listbox with many records and I would like to have the ability to search inside the listbox.

The Listbox has several columns: Column 1 is the CustomerID, column 2 is the name and so on.

screengrab
The following code searches inside the listbox, but only in the first column - which is the ID.
How do I change the code so that it searches inside the second column?
Private Sub txtSearch_Change()
  Dim strText As String
  Dim i As Long
  
  strText = LCase(txtSearch.Text)
  With ListBox1
    For i = 0 To .ListCount - 1
      If LCase(Left$(.List(i), Len(strText))) = strText Then Exit For
    Next i
    If i = .ListCount Then
      ' No matching item was found, select nothing
      .ListIndex = -1
    Else
      ' A match was found, select it
      .ListIndex = i
    End If
  End With
End Sub

Open in new window


Is it also possible to filter the listbox as I type?

Thanks
example.xlsm
0
Comment
Question by:Massimo Scola
  • 2
5 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41733032
Hi,

pls try

Private Sub txtSearch_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

  Dim strText As String
  Dim i As Long
  
  strText = LCase(txtSearch.Text)
  With ListBox1
    For i = 0 To .ListCount - 1
      If LCase(Left$(.List(i, 1), Len(strText))) = strText Or LCase(Left$(.List(i, 2), Len(strText))) = strText Then Exit For
    Next i
    If i = .ListCount Then
      ' No matching item was found, select nothing
      .ListIndex = -1
    Else
      ' A match was found, select it
      .ListIndex = i
    End If
  End With
End Sub

Open in new window

Regards
0
 

Author Comment

by:Massimo Scola
ID: 41733088
OK this works.  I have two questions

1. )Is there something like a wildcard operator which I can use if I only know part of the name?

2) If I want to filter the data, would I have to make use of arrays? Or what is the approach?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41733180
1) instead of Left use Instr
InStr(LCase(.List(i, 1)), strText)

Open in new window


2)then try
Private Sub txtSearch_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Dim rw
Dim strText As String

strText = LCase(txtSearch.Text)
Dim rng As Range
Set rng = Range("Customers")
With ListBox1
    .RowSource = ""
    .ColumnCount = 3
    For Each rw In rng.Rows
        If InStr(LCase(Cells(rw.Row, 2)), strText) Or InStr(LCase(Cells(rw.Row, 3)), strText) Then
            .AddItem Cells(rw.Row, 1).Value
            .List(ListBox1.ListCount - 1, 1) = Cells(rw.Row, 2).Value
            .List(ListBox1.ListCount - 1, 2) = Cells(rw.Row, 3).Value
        End If
    Next
End With
End Sub

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question