Macro to Filter by Number

ghp2017

Board Regular
Joined
Dec 12, 2017
Messages
51
For some I can get it to search by text but when I try a number it filters everything out like theres no match.

Dim strName As String
strName = InputBox("What number would you like to search for?")
Selection.AutoFilter Field:=4, Criteria1:="=*" & strName & "*", Operator:=xlAnd
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Are you able to manually create a filter to search for the number you want, and have it work the way you want?
If so, then turn on your Macro Recorder and record yourself manually creating that filter.
Then stop the Macro Recorder, and take a look at the code you just recorded. That is what you need to build your VBA code to look like.
 
Upvote 0
If you are going to filter only numbers.
Assuming you have the headers in row1 and want to filter on column D, try the following:

VBA Code:
Sub testfilter()
  Dim rng As Range, c As Range
  Dim strName As String
  Dim dic As Object
  Application.ScreenUpdating = False
  
  strName = InputBox("What number would you like to search for?")
  
  Set dic = CreateObject("Scripting.Dictionary")
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  Set rng = Range("A1", Range("D" & Rows.Count).End(3))
  
  For Each c In rng.Columns(4).Cells
    If InStr(1, c.Text, strName) > 0 Then
      dic(c.Text) = Empty
    End If
  Next
  If dic.Count > 0 Then
    rng.AutoFilter 4, dic.keys, xlFilterValues
  Else
    MsgBox "There is no data to filter"
  End If
  
  Application.ScreenUpdating = True
End Sub

It even works with texts too.

🧙‍♂️
 
Last edited:
Upvote 0
If you are going to filter only numbers.
Assuming you have the headers in row1 and want to filter on column D, try the following:

VBA Code:
Sub testfilter()
  Dim rng As Range, c As Range
  Dim strName As String
  Dim dic As Object
  Application.ScreenUpdating = False
 
  strName = InputBox("What number would you like to search for?")
 
  Set dic = CreateObject("Scripting.Dictionary")
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  Set rng = Range("A1", Range("D" & Rows.Count).End(3))
 
  For Each c In rng.Columns(4).Cells
    If InStr(1, c.Text, strName) > 0 Then
      dic(c.Text) = Empty
    End If
  Next
  If dic.Count > 0 Then
    rng.AutoFilter 4, dic.keys, xlFilterValues
  Else
    MsgBox "There is no data to filter"
  End If
 
  Application.ScreenUpdating = True
End Sub

It even works with texts too.

🧙‍♂️
Thanks!!
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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