xoScarecrowox
New Member
- Joined
- Apr 2, 2019
- Messages
- 15
Hi There, I have a code that allows the user to first select a column by choosing header name listed in ComboBox44 and then enter text to search in TextBox1 and displays search results as a row in Listbox1 with headers defined in first part of code , the code runs when I click CommandButton1
All works well except I would like the search results to start showing in ListBox1 as soon as text is entered into TextBox1, for example
Choose
Chosen
When I type "Cho" Both would display until the 4th letter typed which would then remove the other from Listbox1, would prefer if search was started from left side of search word
here is my code
Option Compare Text
Private Sub CommandButton1_Click()
Dim a
Dim oCell As Range
Dim sRng As Range
Dim rng1 As Range
Dim y As Worksheet
'Fills header Row in listbox
Set y = Sheets("Sheet1")
a = Sheets("Sheet1").Range("A1:AR1")
With Me.ListBox1
.List = a
.ColumnCount = UBound(a, 2)
End With
'If ComboxBox44 is blank then display message
If ComboBox44 = "" Then
MsgBox "You have not chosen search column", vbOKOnly
Exit Sub
'Check ComboBox44 for chosen text and sets search column
'Pulldown list defined in Rowsource properties for ComboBox44
ElseIf ComboBox44 = "Age" Then
Set rng1 = Sheets("sheet1").Range("B2:B100") 'Sets B as search column
ElseIf ComboBox44 = "Surname" Then
Set rng1 = Sheets("sheet1").Range("C2:C100") 'Sets C as search column
ElseIf ComboBox44 = "First Name" Then
Set rng1 = Sheets("sheet1").Range("D2:D100") 'Sets D as search column
End If
'Display message if TextBox1 is blank
If TextBox1 = "" Then
MsgBox "You have not entered search word", vbOKOnly
Exit Sub
End If
'Searches each row for the word in TextBox1 and copies each row to Listbox1
For Each oCell In rng1
If oCell.Text = TextBox2.Value Then
With oCell.EntireRow
ListBox1.AddItem Value
ListBox1.List(ListBox1.ListCount - 1, 0) = .Range("A1").Value
ListBox1.List(ListBox1.ListCount - 1, 1) = .Range("B1").Value
ListBox1.List(ListBox1.ListCount - 1, 2) = .Range("C1").Value
ListBox1.List(ListBox1.ListCount - 1, 3) = .Range("D1").Value
ListBox1.List(ListBox1.ListCount - 1, 4) = .Range("E1").Value
ListBox1.List(ListBox1.ListCount - 1, 5) = .Range("F1").Value
ListBox1.List(ListBox1.ListCount - 1, 6) = .Range("G1").Value
ListBox1.List(ListBox1.ListCount - 1, 7) = .Range("H1").Value
ListBox1.List(ListBox1.ListCount - 1, 8) = .Range("I1").Value
ListBox1.List(ListBox1.ListCount - 1, 9) = .Range("J1").Value
ListBox1.List(ListBox1.ListCount - 1, 10) = .Range("K1").Value
ListBox1.List(ListBox1.ListCount - 1, 11) = .Range("L1").Value
ListBox1.List(ListBox1.ListCount - 1, 12) = .Range("M1").Value
ListBox1.List(ListBox1.ListCount - 1, 13) = .Range("N1").Value
ListBox1.List(ListBox1.ListCount - 1, 14) = .Range("O1").Value
ListBox1.List(ListBox1.ListCount - 1, 15) = .Range("P1").Value
ListBox1.List(ListBox1.ListCount - 1, 16) = .Range("Q1").Value
ListBox1.List(ListBox1.ListCount - 1, 17) = .Range("R1").Value
ListBox1.List(ListBox1.ListCount - 1, 18) = .Range("S1").Value
ListBox1.List(ListBox1.ListCount - 1, 19) = .Range("T1").Value
ListBox1.List(ListBox1.ListCount - 1, 20) = .Range("U1").Value
ListBox1.List(ListBox1.ListCount - 1, 21) = .Range("V1").Value
ListBox1.List(ListBox1.ListCount - 1, 22) = .Range("W1").Value
ListBox1.List(ListBox1.ListCount - 1, 23) = .Range("X1").Value
ListBox1.List(ListBox1.ListCount - 1, 24) = .Range("Y1").Value
ListBox1.List(ListBox1.ListCount - 1, 25) = .Range("Z1").Value
ListBox1.List(ListBox1.ListCount - 1, 26) = .Range("AA1").Value
ListBox1.List(ListBox1.ListCount - 1, 27) = .Range("AB1").Value
ListBox1.List(ListBox1.ListCount - 1, 28) = .Range("AC1").Value
ListBox1.List(ListBox1.ListCount - 1, 29) = .Range("AD1").Value
ListBox1.List(ListBox1.ListCount - 1, 30) = .Range("AE1").Value
ListBox1.List(ListBox1.ListCount - 1, 31) = .Range("AF1").Value
ListBox1.List(ListBox1.ListCount - 1, 32) = .Range("AG1").Value
ListBox1.List(ListBox1.ListCount - 1, 33) = .Range("AH1").Value
ListBox1.List(ListBox1.ListCount - 1, 34) = .Range("AI1").Value
ListBox1.List(ListBox1.ListCount - 1, 35) = .Range("AJ1").Value
ListBox1.List(ListBox1.ListCount - 1, 36) = .Range("AK1").Value
ListBox1.List(ListBox1.ListCount - 1, 37) = .Range("AL1").Value
ListBox1.List(ListBox1.ListCount - 1, 38) = .Range("AM1").Value
ListBox1.List(ListBox1.ListCount - 1, 39) = .Range("AN1").Value
ListBox1.List(ListBox1.ListCount - 1, 40) = .Range("AO1").Value
ListBox1.List(ListBox1.ListCount - 1, 41) = .Range("AP1").Value
ListBox1.List(ListBox1.ListCount - 1, 42) = .Range("AQ1").Value
ListBox1.List(ListBox1.ListCount - 1, 43) = .Range("AR1").Value
End With
End If
Next oCell
If Not sRng Is Nothing Then ListBox1.List = sRng.Value
End Sub
All works well except I would like the search results to start showing in ListBox1 as soon as text is entered into TextBox1, for example
Choose
Chosen
When I type "Cho" Both would display until the 4th letter typed which would then remove the other from Listbox1, would prefer if search was started from left side of search word
here is my code
Option Compare Text
Private Sub CommandButton1_Click()
Dim a
Dim oCell As Range
Dim sRng As Range
Dim rng1 As Range
Dim y As Worksheet
'Fills header Row in listbox
Set y = Sheets("Sheet1")
a = Sheets("Sheet1").Range("A1:AR1")
With Me.ListBox1
.List = a
.ColumnCount = UBound(a, 2)
End With
'If ComboxBox44 is blank then display message
If ComboBox44 = "" Then
MsgBox "You have not chosen search column", vbOKOnly
Exit Sub
'Check ComboBox44 for chosen text and sets search column
'Pulldown list defined in Rowsource properties for ComboBox44
ElseIf ComboBox44 = "Age" Then
Set rng1 = Sheets("sheet1").Range("B2:B100") 'Sets B as search column
ElseIf ComboBox44 = "Surname" Then
Set rng1 = Sheets("sheet1").Range("C2:C100") 'Sets C as search column
ElseIf ComboBox44 = "First Name" Then
Set rng1 = Sheets("sheet1").Range("D2:D100") 'Sets D as search column
End If
'Display message if TextBox1 is blank
If TextBox1 = "" Then
MsgBox "You have not entered search word", vbOKOnly
Exit Sub
End If
'Searches each row for the word in TextBox1 and copies each row to Listbox1
For Each oCell In rng1
If oCell.Text = TextBox2.Value Then
With oCell.EntireRow
ListBox1.AddItem Value
ListBox1.List(ListBox1.ListCount - 1, 0) = .Range("A1").Value
ListBox1.List(ListBox1.ListCount - 1, 1) = .Range("B1").Value
ListBox1.List(ListBox1.ListCount - 1, 2) = .Range("C1").Value
ListBox1.List(ListBox1.ListCount - 1, 3) = .Range("D1").Value
ListBox1.List(ListBox1.ListCount - 1, 4) = .Range("E1").Value
ListBox1.List(ListBox1.ListCount - 1, 5) = .Range("F1").Value
ListBox1.List(ListBox1.ListCount - 1, 6) = .Range("G1").Value
ListBox1.List(ListBox1.ListCount - 1, 7) = .Range("H1").Value
ListBox1.List(ListBox1.ListCount - 1, 8) = .Range("I1").Value
ListBox1.List(ListBox1.ListCount - 1, 9) = .Range("J1").Value
ListBox1.List(ListBox1.ListCount - 1, 10) = .Range("K1").Value
ListBox1.List(ListBox1.ListCount - 1, 11) = .Range("L1").Value
ListBox1.List(ListBox1.ListCount - 1, 12) = .Range("M1").Value
ListBox1.List(ListBox1.ListCount - 1, 13) = .Range("N1").Value
ListBox1.List(ListBox1.ListCount - 1, 14) = .Range("O1").Value
ListBox1.List(ListBox1.ListCount - 1, 15) = .Range("P1").Value
ListBox1.List(ListBox1.ListCount - 1, 16) = .Range("Q1").Value
ListBox1.List(ListBox1.ListCount - 1, 17) = .Range("R1").Value
ListBox1.List(ListBox1.ListCount - 1, 18) = .Range("S1").Value
ListBox1.List(ListBox1.ListCount - 1, 19) = .Range("T1").Value
ListBox1.List(ListBox1.ListCount - 1, 20) = .Range("U1").Value
ListBox1.List(ListBox1.ListCount - 1, 21) = .Range("V1").Value
ListBox1.List(ListBox1.ListCount - 1, 22) = .Range("W1").Value
ListBox1.List(ListBox1.ListCount - 1, 23) = .Range("X1").Value
ListBox1.List(ListBox1.ListCount - 1, 24) = .Range("Y1").Value
ListBox1.List(ListBox1.ListCount - 1, 25) = .Range("Z1").Value
ListBox1.List(ListBox1.ListCount - 1, 26) = .Range("AA1").Value
ListBox1.List(ListBox1.ListCount - 1, 27) = .Range("AB1").Value
ListBox1.List(ListBox1.ListCount - 1, 28) = .Range("AC1").Value
ListBox1.List(ListBox1.ListCount - 1, 29) = .Range("AD1").Value
ListBox1.List(ListBox1.ListCount - 1, 30) = .Range("AE1").Value
ListBox1.List(ListBox1.ListCount - 1, 31) = .Range("AF1").Value
ListBox1.List(ListBox1.ListCount - 1, 32) = .Range("AG1").Value
ListBox1.List(ListBox1.ListCount - 1, 33) = .Range("AH1").Value
ListBox1.List(ListBox1.ListCount - 1, 34) = .Range("AI1").Value
ListBox1.List(ListBox1.ListCount - 1, 35) = .Range("AJ1").Value
ListBox1.List(ListBox1.ListCount - 1, 36) = .Range("AK1").Value
ListBox1.List(ListBox1.ListCount - 1, 37) = .Range("AL1").Value
ListBox1.List(ListBox1.ListCount - 1, 38) = .Range("AM1").Value
ListBox1.List(ListBox1.ListCount - 1, 39) = .Range("AN1").Value
ListBox1.List(ListBox1.ListCount - 1, 40) = .Range("AO1").Value
ListBox1.List(ListBox1.ListCount - 1, 41) = .Range("AP1").Value
ListBox1.List(ListBox1.ListCount - 1, 42) = .Range("AQ1").Value
ListBox1.List(ListBox1.ListCount - 1, 43) = .Range("AR1").Value
End With
End If
Next oCell
If Not sRng Is Nothing Then ListBox1.List = sRng.Value
End Sub