VBA - Filter before Tomorrow

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
i have recorded a macro and editted so it should filter a table and show anying that is before tomorrows date.

however when I run it it does not give any results.

Code:
Sub overdue()
'
' overdue Macro
'

Dim criteria As Date
criteria = Date + 1
    ActiveSheet.Range("$A$1:$P$188").AutoFilter Field:=12
    ActiveWorkbook.Worksheets("Active rejects").AutoFilter.Sort.SortFields.Clear
    ActiveSheet.Range("$A$1:$P$188").AutoFilter Field:=12, Criteria1:= _
        "<" & criteria, Operator:=xlAnd
    Sheets("Active rejects").Select
End Sub

Oddly if I click the filter arrow and check the details it is all correct and clicking OK gives the results as I expected.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
That's interesting. The code seemed to work correctly for me when my ActiveSheet is named "Active rejects".

Is there some reason why you use the reference ActiveSheet in some places and Sheets("Active rejects") in other places?
 
Upvote 0
activesheet is defaulted when recording macros

I got rid of all the useless code - Even hard coded in tomorrows date.

Code:
Sub overdue()
    Worksheets("Active rejects").AutoFilter.Sort.SortFields.Clear
    Worksheets("Active rejects").Range("$A$2:$P$188").AutoFilter Field:=12, Criteria1:="<29/07/2011"
End Sub

and it still filters to no records.

However again if I go through the filter manually the "BEFORE" and "29/07/11" is already prefilled in the correct boxes and clicking OK gives the records I need.
 
Upvote 0
Dates are often a bit tricky because of the different world formats. Try making this change
Rich (BB code):
Dim criteria As Long
 
Upvote 0
Thanks for the suggestion but I have removed the "criteria" and gone back to the recorded macro and it still doesn't work

Code:
Sub overdue()
    Worksheets("Active rejects").AutoFilter.Sort.SortFields.Clear
    Worksheets("Active rejects").Range("$A$2:$P$188").AutoFilter Field:=12, Criteria1:="<29/07/2011"
End Sub

I have also tried reapplying the filter after this


Code:
    ActiveSheet.AutoFilter.ApplyFilter

and that didn't work either - Any other suggestions?
 
Upvote 0
Thanks for the suggestion but I have removed the "criteria" and gone back to the recorded macro and it still doesn't work
In that case, why don't you at least try the suggestion? ;)
 
Upvote 0
Thanks Peter.

I appreciate your persistance
 
Upvote 0
My initial thought was that this was a date formatting problem (either in the data range to filter or the criteria), but if that were the case, I don't think this could happen....

Oddly if I click the filter arrow and check the details it is all correct and clicking OK gives the results as I expected.
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,484
Members
452,917
Latest member
MrsMSalt

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