VBA Excel - How to code a filter for a list box search

mikeVA

New Member
Joined
Sep 14, 2014
Messages
4
Hi,

I wanted to make a filter in a text box of a form that returns search results. The search works great. My code for the filter returns a run time error when I make an entry in the text box. Can someone help with a filter code? I want to filter only the fiscal year in column A by entering in the text box, ie FY18. I listed the names of all the elements involved in the code below.


Here are all the elements.


  1. VBA Form: "frmGLSearch" (Name of Form);
  2. VBA Form TextBox:"EnterGL" (TextBox); 3)
  3. VBA Form Button: "Search" (Button);
  4. VBA Form TextBox: "Filter" (TextBox);
  5. VBA Form ListBox: "GLResult" (ListBox);
  6. Worksheet: "General Search" (Tab with Named Range and Dynamic Search);
  7. Worksheet: "Data" (Source Data);
  8. Worksheet: "General" (Tab with Search Button);
  9. Named Range: "GeneralSearch"(Name Range on tab General Search with offset formula)

--VBA Code to Filter with a Text Box on Form--

<code>
Code:
Private Sub Filter_Change()

Dim i As Long
Dim arrList As Variant

Me.GLResult.Clear
If Worksheets("General Search").Range("A" & Worksheets("General Search").Rows.Count).End(xlUp).Row > 1 And Trim(Me.Filter.Value) <> vbNullString Then
    arrList = Worksheets("General Search").Range("A1:A" & Worksheets("General Search").Range("A" & Worksheets("General Search").Rows.Count).End(xlUp).Row).Value2
    For i = LBound(arrList) To UBound(arrList)
        If InStr(1, arrList(i, 1), Trim(Me.Filter.Value), vbTextCompare) Then
            Me.GLResult.AddItem arrList(i, 1)
        End If
    Next i
End If
If Me.GLResult.ListCount = 1 Then Me.GLResult.Selected(0) = True

End Sub
</code>


---Additional VBA----
<code>
Code:
Option Explicit

Private Sub Search_Click()

Dim RowNum As Long
Dim SearchRow As Long

RowNum = 2
SearchRow = 2

Worksheets("Data").Activate

Do Until Cells(RowNum, 1).Value = ""

If InStr(1, Cells(RowNum, 2).Value, EnterGL.Value, vbTextCompare) > 0 Then
Worksheets("General Search").Cells(SearchRow, 1).Value = Cells(RowNum, 1).Value
Worksheets("General Search").Cells(SearchRow, 2).Value = Cells(RowNum, 2).Value
Worksheets("General Search").Cells(SearchRow, 3).Value = Cells(RowNum, 3).Value
Worksheets("General Search").Cells(SearchRow, 4).Value = Cells(RowNum, 4).Value
Worksheets("General Search").Cells(SearchRow, 5).Value = Cells(RowNum, 5).Value
Worksheets("General Search").Cells(SearchRow, 6).Value = Cells(RowNum, 6).Value
Worksheets("General Search").Cells(SearchRow, 7).Value = Cells(RowNum, 7).Value
SearchRow = SearchRow + 1
End If
RowNum = RowNum + 1
Loop

If SearchRow = 2 Then
MsgBox "GL not found"
Exit Sub
End If

GLResult.RowSource = "GeneralSearch"

End Sub

SIKLwEAANsEAAAaAAAAAAAAAAAAAAAAANEKAAB4bC9fcmVscy93b3JrYm9vay54bWwucmVsc1BLAQItABQABgAIAAAAIQD6BJSZeQMAALYKAAAYAAAAAAAAAAAAAAAAAEANAAB4bC93b3Jrc2hlZXRzL3NoZWV0MS54bWxQSwECLQAUAAYACAAAACEAtol5F4ECAAACBgAAGAAAAAAAAAAAAAAAAADvEAAAeGwvd29ya3NoZWV0cy9zaGVldDIueG1sUEsBAi0AFAAGAAgAAAAhANqfxkRuAgAAfwUAABgAAAAAAAAAAAAAAAAAphMAAHhsL3dvcmtzaGVldHMvc2hlZXQzLnhtbFBLAQItABQABgAIAAAAIQB9s6qKIAUAADYYAAAYAAAAAAAAAAAAAAAAAEoWAAB4bC93b3Jrc2hlZXRzL3NoZWV0NC54bWxQSwECLQAUAAYACAAAACEAwRcQvk4HAADGIAAAEwAAAAAAAAAAAAAAAACgGwAAeGwvdGhlbWUvdGhlbWUxLnhtbFBLAQItABQABgAIAAAAIQDUF2GwIgMAAAAIAAANAAAAAAAAAAAAAAAAAB8jAAB4bC9zdHlsZXMueG1sUEsBAi0AFAAGAAgAAAAhAEJj3lfqAQAA6QQAABQAAAAAAAAAAAAAAAAAbCYAAHhsL3NoYXJlZFN0cmluZ3MueG1sUEsBAi0AFAAGAAgAAAAhAK9tPQ8vAwAA3QYAABgAAAAAAAAAAAAAAAAAiCgAAHhsL2RyYXdpbmdzL2RyYXdpbmcxLnhtbFBLAQItABQABgAIAAAAIQDQgdEodjMAAACSAAARAAAAAAAAAAAAAAAAAO0rAAB4bC92YmFQcm9qZWN0LmJpblBLAQItABQABgAIAAAAIQCV9h0cAAEAAI0BAAAjAAAAAAAAAAAAAAAAAJJfAAB4bC93b3Jrc2hlZXRzL19yZWxzL3NoZWV0MS54bWwucmVsc1BLAQItABQABgAIAAAAIQAVr6o8vQAAACsBAAAjAAAAAAAAAAAAAAAAANNgAAB4bC93b3Jrc2hlZXRzL19yZWxzL3NoZWV0Mi54bWwucmVsc1BLAQItABQABgAIAAAAIQCLfF15QQEAAFcCAAARAAAAAAAAAAAAAAAAANFhAABkb2NQcm9wcy9jb3JlLnhtbFBLAQItABQABgAIAAAAIQCFtSfrpQEAAHADAAAQAAAAAAAAAAAAAAAAAElkAABkb2NQcm9wcy9hcHAueG1sUEsFBgAAAAARABEAeQQAACRnAAAAAA==

</code>
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
hi,
you have not stated what the error is but looking at the two codes

In the Filter_Change change code try changing this line

Code:
Me.GLResult.Clear

to this

Code:
With Me.GLResult
        .RowSource = ""
        .Clear
    End With

and see if this resolves but if not, let us know the error you are getting

Dave
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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