Why is my Filter Macro not Filtering Correctly?

rob51852

Board Regular
Joined
Jun 27, 2016
Messages
190
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am trying to put together a simple macro to filter a table for rows containing today's date (column 1) and where the value in column 5 is greater than 5. However, when I run it all rows disappear even though there are many that meet the criteria.

Can anyone see what the problem is?

Thanks

VBA Code:
Sub Filter()
ActiveSheet.ListObjects("TTable").Range.AutoFilter Field:=1, Criteria1:=Array(Date), Operator:=xlFilterValues
ActiveSheet.ListObjects("TTable").Range.AutoFilter Field:=5, Criteria2:=">5", Operator:=xlFilterValues
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Dates and filters can be problematic in code. It's usually safer to do something like this:

Code:
Sub Filter()
With ActiveSheet.ListObjects("TTable").Range
   .AutoFilter Field:=1, Criteria1:=">=" & CDbl(Date), Operator:=xlAnd, Criteria2:="<=" & CDbl(Date)
   .AutoFilter Field:=5, Criteria1:=">5"
End With
End Sub
 
Upvote 0
Thanks Rory. I still get the same result unfortunately.
 
Upvote 0
Are you sure there aren't any time parts in the date column that are hidden by formatting? If there are, use:

VBA Code:
Sub Filter()
With ActiveSheet.ListObjects("TTable").Range
   .AutoFilter Field:=1, Criteria1:=">=" & CDbl(Date), Operator:=xlAnd, Criteria2:="<" & CDbl(Date) + 1
   .AutoFilter Field:=5, Criteria1:=">5"
End With
End Sub
 
Upvote 0
Hi,​
do it manually and once it works, cancel the filter and activate the Macro Recorder and redo the same operation in order to compare with the generated code …​
 
Upvote 0
Thanks again Rory. For some reason the filter is greyed out. I've tried formatting the column as dates but it's still grey
 
Upvote 0
What exactly is greyed out?
 
Upvote 0
Untitled.png


I've closed and reopened the workbook and now it just give sort A to Z as the option. It's the same when I go to the Data tab and try sorting from there
 
Upvote 0
It looks like you have some non-date entries in there, since you have Text filters as an option, not Date filters.
 
Upvote 0
Strange one. There's no text in there. I'll come up with a workaround.

Thanks for all your help Rory.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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