how to add textbox as search for listbox?

raz355

New Member
Joined
Feb 8, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
how can I add textbox as search to this code. I need it to search for a single column for different textbox, for example textbox1 search in column C, textbox2 search in column E, and textbox3 search in column K

VBA Code:
Private Sub txt_sn1_Change()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim Cell As Range
    Dim r As Integer
    Set Sh = Worksheets("Systemtest")
    With Sh
        Set Rng = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
    End With

      ReDim b(1 To 1, 1 To 11)  '
      With ListBox1
        .ColumnCount = 11       '
        .List = b               '
        .Clear                  '
      End With
    '
    r = 0
    For Each Cell In Rng
        With Cell
            ListBox1.AddItem .Value
            ListBox1.List(r, 1) = .Offset(0, 1).Value
            ListBox1.List(r, 2) = .Offset(0, 2).Value
            ListBox1.List(r, 3) = .Offset(0, 3).Value
            ListBox1.List(r, 4) = .Offset(0, 4).Value
            ListBox1.List(r, 5) = .Offset(0, 5).Value
            ListBox1.List(r, 6) = .Offset(0, 6).Value
            ListBox1.List(r, 7) = .Offset(0, 7).Value
            ListBox1.List(r, 8) = .Offset(0, 8).Value
            ListBox1.List(r, 9) = .Offset(0, 9).Value
            ListBox1.List(r, 10) = .Offset(0, 10).Value
          End With
        r = r + 1
    Next Cell
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi and welcome to MrExcel!

For these types of searches, I show you an alternative. Replace all your code with the following code.

Change in the code in these lines the names of the textboxes (in red, green and blue colors) by the names of your textboxes
Rich (BB code):
    If txt_sn1 = "" Then txt1 = a(i, 3) Else txt1 = txt_sn1     'textbox1 search in C
    If txt_sn2 = "" Then txt2 = a(i, 5) Else txt2 = txt_sn2     'textbox2 search in E
    If txt_sn3 = "" Then txt3 = a(i, 11) Else txt3 = txt_sn3    'textbox3 search in K

The code loads in an array 'a' all the information from the sheet. Every time you enter data in any of the 3 textboxes, it automatically performs a filter of the information, but in memory, it passes the data that matches to array 'b' and then loads listbox1 with the data from array 'b' .

Maybe it is necessary to make some adjustment in the search, since I am only considering that columns C, E and K have texts, but if it has numbers or dates then the type of search will have to be adjusted.

Test the code with text data to get familiar with searching, and then we'll make adjustments as needed.

VBA Code:
Option Explicit     'These lines at the beginning of all the code
Dim a As Variant

Private Sub txt_sn1_Change()   'Fit to the name of your textbox
  Call Filter_Data
End Sub
Private Sub txt_sn2_Change() 'Fit to the name of your textbox
  Call Filter_Data
End Sub
Private Sub txt_sn3_Change() 'Fit to the name of your textbox
  Call Filter_Data
End Sub

Sub Filter_Data()
  Dim i As Long, j As Long, k As Long
  Dim txt1 As String, txt2 As String, txt3 As String
  Dim b As Variant
 
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 1 To UBound(a, 1)
    If txt_sn1 = "" Then txt1 = a(i, 3) Else txt1 = txt_sn1     'textbox1 search in C
    If txt_sn2 = "" Then txt2 = a(i, 5) Else txt2 = txt_sn2     'textbox2 search in E
    If txt_sn3 = "" Then txt3 = a(i, 11) Else txt3 = txt_sn3    'textbox3 search in K
  
    'search: textbox1 in C, textbox2 in E, and textbox3 in K
    If LCase(a(i, 3)) Like "*" & LCase(txt1) & "*" And _
       LCase(a(i, 5)) Like "*" & LCase(txt2) & "*" And _
       LCase(a(i, 11)) Like "*" & LCase(txt3) & "*" Then
      k = k + 1
      For j = 1 To UBound(a, 2)
        b(k, j) = a(i, j)
      Next
    End If
  Next
  ListBox1.List = b
End Sub

Private Sub UserForm_Activate()
  Dim sh As Worksheet
  Set sh = Worksheets("Systemtest")
  a = sh.Range("A2:K" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
  With ListBox1
    .ColumnCount = UBound(a, 2)
    .List = a
  End With
End Sub
 
Upvote 1
Solution
Its exactly how I need it to be, thanks alot for the help. Btw is there anyway i can show the header in row A1. I try to change sh.range("A1:K....... But when I start type in textbox its disappear and show A2 and so on.
 
Upvote 0
Also can you show me how the code is if the case were for date or integer(for learning purposes)
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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