Search by textbox and criteria combobox and show in listbox

mredvard

Banned user
Joined
Nov 17, 2023
Messages
7
Hello, good day everyone, I want to search based on the combobox as (search column) and textbox in sheet 1.
How is this possible?


search by combo box.xlsm
ABCDEFGH
1no.Nameidsexagedate of birthIQ TestMeaning of Name
214Ani4935426663female2231/10/199998Ornament, Beautiful, Variant of Anne / Ane
315Anna4037366181female2026/10/2001126‘Grace’. Also used as a nickname for Annabelle
416Anya9330871997female2512/04/1997109Inexhaustible, Gracious, Graceful
517Ariel3660975387female2527/01/1997127‘Lion of God’. It is also spelt as Arielle.
618Arya9935838857female2317/07/1999108‘Lioness, Noble’. It is frequently spelt as Aria.
719Ash3882405562female2514/12/199696Form of Ashley, Ash Tree Meadow
820Ashley7564224317female2603/03/199699‘One who lives in the ash tree grove’. It is one of the modern English baby girl names.
921Audrey4121552927female1804/11/2003111Strength
1022Ava6560141181female1929/10/2002124‘Bird’ or ‘the living one’ or ‘life’. It may also be spelt as Eva.
1123Badra8004003618female2710/05/1995114Full Moon, Pretty, Goddess Durga
1224Beatrice6870820925female2011/02/200291‘She who makes you happy’. It is also spelt Beatrix.
1325Belinda3658130608female2409/06/1998119‘Beautiful’. One of its other variants is ‘Bella’. ‘Bright linden tree’ or ‘bright serpent’.
1426Bella2725479507female1820/09/2003122‘Beautiful’. It is often used as a nickname for other names ending in –bella, like Annabella, Arabella, etc.
1527Bency5191837585female2606/04/1996111Right Hand of Jesus Christ, A Talkative Girl who is Very Scared
1628Berey4061161891female2012/05/2002107Exalted One, Bearer of Victory, True Image, To Help, Berry
1729Bery1556003492female2214/12/1999129True Image, Bearer of Victory, Berry, To Help, Exalted One
1830Bety2871809184female2001/09/2002119God is My Oath, God is Perfection
1931Bitsy2218154671female2217/11/1999109God is Perfection, God is My Oath
2032Brianna5026781508female2120/08/2001123‘Strong’
sheet1
 

Attachments

  • search form.png
    search form.png
    6.5 KB · Views: 8
  • sh1.png
    sh1.png
    65.3 KB · Views: 9

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi and welcome to MrExcel

Check this:



🧙‍♂️
 
Upvote 0
hi bro tank u for answer
I made this based on what you said, the form runs without errors
But it does not search for anything
When something is selected from the list box or when something is written in the text box, nothing happens in the list box

what is the reason?

i
VBA Code:
Option Explicit

Dim a As Variant

Private Sub Combobox1_change()
  Call FilterData
End Sub

Private Sub TextBox1_Change()
  Call FilterData
End Sub

Sub FilterData()
  Dim cmb As String, txt As String
  Dim b As Variant
  Dim i As Long, j As Long
 
  ReDim b(1 To UBound(a), 1 To 2)
  ListBox1.Clear
  For i = 1 To UBound(a)
    If ComboBox1.Value = "" Then cmb = a(i, 4) Else cmb = ComboBox1.Value
    If TextBox1.Value = "" Then txt = a(i, 1) Else txt = TextBox1.Value
    If a(i, 4) = ComboBox1.Value And LCase(a(i, 1)) Like LCase(txt) & "*" Then
      j = j + 1
      b(j, 1) = a(i, 1)
      b(j, 2) = a(i, 4)
    End If
  Next i
  If j > 0 Then ListBox1.List = b
End Sub

Private Sub userform_initialize()
  Dim ws As Worksheet
    Dim comboValues As Variant
    Set ws = ThisWorkbook.Sheets("sheet1")
    Dim lastRow As Long
    Dim dataRange As Range

     ComboBox1.List = Array(ws.Cells(1, 1).Value, ws.Cells(1, 2).Value, ws.Cells(1, 3).Value, ws.Cells(1, 4).Value, ws.Cells(1, 5).Value, ws.Cells(1, 6).Value, ws.Cells(1, 7).Value, ws.Cells(1, 8).Value)
ListBox1.ColumnCount = 4

lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

Set dataRange = ws.Range("A2:H" & lastRow)
a = dataRange.Value
End Sub
 
Upvote 0

@DanteAmor

I want to select the search condition based on the list box and then enter the value in the text box and after search result show in listbox
 
Upvote 0
You need to adapt the code to the columns where you are going to perform the search.

In which column do you want to search for what you select in the combobox?

In which column do you want to search for what you select in the Textbox?
 
Upvote 0
I want to select the search condition based on the list box and then enter the value in the text box and after search result show in listbox
Replace all your code for this:

VBA Code:
Option Explicit

Dim a As Variant

Private Sub Combobox1_change()
  Call FilterData
End Sub

Private Sub TextBox1_Change()
  Call FilterData
End Sub

Sub FilterData()
  Dim cmb As String, txt As String
  Dim b As Variant
  Dim i As Long, j As Long, k As Long, col As Long
 
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  ListBox1.Clear
  If ComboBox1.ListIndex = -1 Then Exit Sub
  
  col = ComboBox1.ListIndex + 1
  For i = 1 To UBound(a)
    If TextBox1.Value = "" Then txt = a(i, col) Else txt = TextBox1.Value
    If LCase(a(i, col)) Like "*" & LCase(txt) & "*" Then
      k = k + 1
      For j = 1 To 8
        b(k, j) = a(i, j)
      Next
    End If
  Next i
  If k > 0 Then ListBox1.List = b
End Sub

Private Sub userform_initialize()
  Dim ws As Worksheet
  
  Set ws = ThisWorkbook.Sheets("sheet1")
  ComboBox1.List = Application.Transpose(ws.Range("A1:H1").Value)
  ListBox1.ColumnCount = 8
  a = ws.Range("A2:H" & ws.Range("A" & Rows.Count).End(xlUp).Row).Value
End Sub

🫡
 
Upvote 1
Solution
Hello، my good friend.
If I don't want to show anything in the listbox ,until I select the Combobox and enter the word in text box, where should I change the code?
when i select one item in combobox the list box is populate
i used this but its not work
VBA Code:
 If k > 0 And textbox1.value> 1 Then ListBox1.List = b

Is it possible to do something that if something is inserted in the textbox، it will be in the whole column، and if the combo box is selected، it will search only in that column.
 
Last edited:
Upvote 0
If I don't want to show anything in the listbox ,until I select the Combobox and enter the word in text box
For that.
After this line:
If ComboBox1.ListIndex = -1 Then Exit Sub

Add this line:
If TextBox1.Value = "" Then Exit Sub


Is it possible to do something that if something is inserted in the textbox، it will be in the whole column، and if the combo box is selected، it will search only in that column.
The search already does that, it only searches the column selected in the combo.
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,077
Members
449,094
Latest member
mystic19

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