Multi-Criteria Search Box for dynamic listbox

bmu13

New Member
Joined
Aug 14, 2017
Messages
3
I am using 2 textboxes, 1 listbox, 1 button. I simply want to show a list that contain what is entered in the textboxes.

textbox1 = color
textbox2 = shape

TEXTBOX 1TEXTBOX 2
ABC
1BLUE-CIRCLEBLUECIRCLE
2GREEN-TRIANGLEGREENTRIANGLE
3RED-TRIANGLEREDTRIANGLE
4RED-SQUAREREDSQUARE
5BLUE-SQUAREBLUESQUARE
6RED-TRIANGLEREDTRIANGLE
7GREEN-CIRCLEGREENCIRCLE
8GREEN-SQUAREGREENSQUARE
9GREEN-SQUAREGREENSQUARE

<tbody>
</tbody>

What I have:
What works:

If textbox1.value <> "" and textbox2.value = "" then
set rng = .range("B1:B9")
set fnd = rng.Find(textbox1.value)
Listbox1.clear
if fnd is nothing then ListBox1.AddItem "Result Not Found": Exit Sub
Set first = fnd
With Listbox1
Listbox1.clear
Listbox1.AddItem fnd.Offset(, -1)
Do
Set fnd = rng.FindNext(fnd)
If fnd.Address = First.Address then Exit Do
ListBox1.AddItem fnd.Offset(,-1)
Loop
End With

What doesn't work

Elseif textbox1.value <> "" and textbox2.value <> "" then
set rng.Range("A1:C9")
ctr1 = 1
For i = 1 To 9
If StrComp(textbox1.value, rng(i, 2)) <> 0 then
arr1(ctr1) = i
ctr1 = ctr1 + 1
End If
Next i
ctr2 = 1
For j = 1 To Len(arr1)
If StrComp(textbox2.value, rng(arr1(j), 3)) = 0 then
arr2(ctr2) = arr1(j)
output(ctr2, 1) = rng(arr1(j), 3)
ctr2 = ctr2 + 1
End if
Next j
Listbox1.clear
Listbox1.List = output
End if



What I want:

textbox1.value = red textbox1.value = red
textbox2.value = "" textbox2.value = triangle


ABC
3RED-TRIANGLEREDTRIANGLE
4RED-SQUAREREDSQUARE
6RED-TRIANGLEREDTRIANGLE

<tbody>
</tbody>
ABC
3RED-TRIANGLEREDTRIANGLE
6RED-TRIANGLEREDTRIANGLE

<tbody>
</tbody>



Elseif




Listbox1
Red-Triangle
Red-Square
Red-Triangle

<tbody>
</tbody>

Elseif
Listbox1
Red-Triangle
Red-Triangle

<tbody>
</tbody>
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG15Aug09
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Ray(), c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("B1"), Range("B" & Rows.Count).End(xlUp))
ListBox1.Clear
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] UCase$(Dn.Value) = UCase$(TextBox1.Value) And UCase$(TextBox2.Value) = UCase$(Dn.Offset(, 1).Value) [COLOR="Navy"]Then[/COLOR]
            c = c + 1
            ReDim Preserve Ray(c)
            Ray(c) = Dn.Offset(, -1)
        [COLOR="Navy"]ElseIf[/COLOR] UCase$(Dn.Value) = UCase$(TextBox1.Value) And UCase$(TextBox2.Value) = "" [COLOR="Navy"]Then[/COLOR]
            c = c + 1
            ReDim Preserve Ray(c)
           Ray(c) = Dn.Offset(, -1)
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
ListBox1.List = Application.Transpose(Ray)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Mick, thank you! Your code worked perfectly. Now, the only thing missing is the scenario in which the user inputs a value that doesn't exist within the range. For example, if textbox1 = pink or textbox2 = cube. then the listbox should say "Result Not Found". How would you make this slight modification?
 
Upvote 0
Try Changing the End of the code as shown in Red:-
Code:
Next Dn
[B][COLOR=#FF0000]If c > 0 Then
    ListBox1.List = Application.Transpose(Ray)
Else
     MsgBox "No Data Found!!"
End If[/COLOR][/B]
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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