VBA filter search box and clear button for numbers

dancer5

New Member
Joined
Jan 6, 2023
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello,
I've been scouring the internet on how to create a search textbox with a clear button that will search one column in my table that has numerical data and filter the table for that. I keep running into issues with it not working for numbers or not being able to clear the search box or not working at all. As you can see, I did try to use a helper column to store the number as text but it wouldn't run.

The last iteration that did work somewhat (only worked for letters, but need it to work for numbers as data will be numbers:
VBA Code:
Private Sub TextBox1_Change()
    Application.ScreenUpdating = False
   
    ActiveSheet.ListObjects("ScreenLog").Range.AutoFilter Field:=2, Criteria1:=[C2] & "*", Operator:=xlFilterValues
    Application.ScreenUpdating = True
End Sub

This is what I was hoping to get working, with a clear button that would clear the filters and clear the textbox, but it doesn't work:
VBA Code:
Sub ClearSearch_Click()
Range("C2").Select
    Selection.ClearContents
    ActiveSheet.ListObjects("ScreenLog").Range.AutoFilter Field:=13
End Sub

Sub TextBox1_Change()
Application.ScreenUpdating = False
   
    ActiveSheet.ListObjects("ScreenLog").Range.AutoFilter Field:=13, Criteria1:="*" & [C2] & "*", Operator:=xlFilterValues
    Application.ScreenUpdating = True
End Sub


Here's my spreadsheet:
1691608834740.png



Screening Log.xlsm
ABCDEFGHIJKLMN
1
2
3
4NameMon/Yr of Birth
5Screen IDID NumberFirstLastInitialsMonthYearAcronymEmployee NameDate of ApproachAgreedIf yes, Actual IDID as Text
6S001153871515387
7S0025486745255486745
8S00315625156
9S004158158
10S005150045150045
11S00644
12S0071526415264
Sheet1
Cell Formulas
RangeFormula
N6:N12N6=LET(x, [@[ID Number]], IF(x=0, "", x))
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try the following to filter numbers. Column N is no longer needed.

VBA Code:
Sub TextBox1_Change()
  Dim tbl As ListObject
  Dim a, b()
  Dim i As Long, n As Long
  
  
  Application.ScreenUpdating = False
  
  Set tbl = ActiveSheet.ListObjects("ScreenLog")
  a = tbl.DataBodyRange
  
  For i = 1 To UBound(a, 1)
    If InStr(1, a(i, 2), TextBox1.Value) > 0 Then
      ReDim Preserve b(n)
      b(n) = CStr(a(i, 2))
      n = n + 1
    End If
  Next
  
  tbl.Range.AutoFilter Field:=2, Criteria1:=b, Operator:=xlFilterValues
  Application.ScreenUpdating = True
End Sub

Sub ClearSearch_Click()
  ActiveSheet.ListObjects("ScreenLog").Range.AutoFilter Field:=2
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Upvote 0
Solution
Thanks for looking into my issue!

For some reason it wouldn't work in my current workbook, the textbox and button weren't clickable, most likely something I did when trying to solve my issues but when I started a fresh workbook they worked.

The only remaining action would be when clicking the clear button, it doesn't clear the textbox, is this possible to do?
 
Upvote 0
it doesn't clear the textbox, is this possible to do?

You mean this:
VBA Code:
Sub ClearSearch_Click()
  TextBox1.Value  = ""
  ActiveSheet.ListObjects("ScreenLog").Range.AutoFilter Field:=2
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,066
Members
449,090
Latest member
fragment

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