populate data in listbox based on multiple textboxes instead one textbox

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
726
Office Version
  1. 2019
hi

I need modified this code to become searching based on multiple textboxes (textbox1,2,3)= COLS( D,E,F) . currently the code works based on COL D with textbox1

VBA Code:
Private Sub TextBox1_Change()
    Dim myArray, lr, x, i
    Dim DATA As Worksheet
    Set DATA = Worksheets("purchase")
    lr = DATA.Cells(Rows.Count, 4).End(xlUp).Row
    ListBox1.Clear
    If TextBox1.Text = "" Then Exit Sub
    myArray = DATA.Range("A2:G" & lr + 1)
    ReDim y(1 To UBound(myArray) * 7, 1 To 7)
    For i = LBound(myArray) To UBound(myArray)
     a = Len(Me.TextBox1.Text)
     For x = 1 To 7
     If Left(myArray(i, x), a) = Left(TextBox1.Text, a) Then
            rw = rw + 1
            For yy = 1 To 7
                y(rw, yy) = myArray(i, yy)
            Next yy
        End If
        Next
    Next i
    If rw > 0 Then
        ListBox1.List = y()
    End If
End Sub

any help would be appreciate
 
currently the code works based on COL D with textbox1
That is not true, the code checks the columns from A to G and if any of them match then it adds the record to the array, but also if 1 or more columns match then it duplicates the record in the array, that is, if column B and column D match the data in textbox1, so add the same record 2 times to listbox.
Do you want the code to keep doing the same for textboxes 2 and 3?
Or do you really want textbox1 to compare with column D, 2 with E, and 3 with F?
 
Upvote 0
You could explain it with examples.
I don't understand what data you have in the textbox and what data you have in the cells and what data will finally be in the listbox.
 
Upvote 0
thanks Dante for follow my thread .
the picture 1 is data in sheet purchase
search textbox.xlsm
ABCDEFG
1DATE CLIENT NOINVOICE NOBRANDTYPEORIGINBALANCE
21/5/2021CUS-BS-1INV-BS-11200R20G580JAP60
31/6/2021CUS-BS-1INV-BS-11200R20G580THI10
41/7/2021CUS-BS-1INV-BS-11200R20R187JAP5
51/8/2021CUS-BS-1INV-BS-11200R20R187THI10
61/9/2021CUS-BS-2INV-BS-21200R24G580JAP5
71/10/2021CUS-BS-2INV-BS-21200R20G580JAP30
81/11/2021CUS-BS-2INV-BS-21200R20G580THI50
91/12/2021CUS-BS-2INV-BS-21400R20VSJJAP12
101/13/2021CUS-BS-2INV-BS-21400R20R180JAP13
PURCHASE



and this is when fill three textboxes toghether based on COLS D,E,F
2.PNG

note : you see stars in the sheet . this is short wort for JAPAN I no know when use Xl2BB TOOLS shows stars
 
Upvote 0
Put all the code inside your user form:

VBA Code:
Option Explicit
Dim a As Variant

Private Sub TextBox1_Change()
  Call FilterData
End Sub

Private Sub TextBox2_Change()
  Call FilterData
End Sub

Private Sub TextBox3_Change()
  Call FilterData
End Sub

Sub FilterData()
  Dim txt1 As String, txt2 As String, txt3 As String
  Dim i As Long, j As Long, k As Long
  
  ListBox1.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 1 To UBound(a)
    If TextBox1 = "" Then txt1 = a(i, 4) Else txt1 = TextBox1
    If TextBox2 = "" Then txt2 = a(i, 5) Else txt2 = TextBox2
    If TextBox3 = "" Then txt3 = a(i, 6) Else txt3 = TextBox3
    If LCase(a(i, 4)) Like LCase(txt1) & "*" And _
       LCase(a(i, 5)) Like LCase(txt2) & "*" And _
       LCase(a(i, 6)) Like LCase(txt3) & "*" Then
      k = k + 1
      For j = 1 To 7
        b(k, j) = a(i, j)
      Next
    End If
  Next
  If k > 0 Then ListBox1.List = b
End Sub

Private Sub UserForm_Activate()
  a = Sheets("purchase").Range("A2:G" & Sheets("purchase").Range("D" & Rows.Count).End(3).Row).Value
End Sub
 
Upvote 0
Solution

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