Autofilter - What's going wrong?

mwperkins

Board Regular
Joined
Oct 29, 2002
Messages
156
Hello,
I am trying to use autofilter to only show records with a date set for during the next 3 weeks.

Two problems:
1. If Date+21 is during the following month, VBA/autofilter appears to try and ignore information saved in local UK date format dd/mm/yyyy and tries to use USA date format mm/dd/yyyy.

2. It does not display data that meets the filter criteria.

Can anyone help?

Example Data
due within 21 days.xls
ABCD
1TaskDueDate
2A17/03/2003
3B18/03/2003
4C19/03/2003
5D20/03/2003
6E21/03/2003
7F22/03/2003
8G23/03/2003
9H24/03/2003
10I06/04/2003
11J07/04/2003
12K08/04/2003
13L09/04/2003
14M10/04/2003
15N11/04/2003
Sheet1


Code I am Using

Code:
Sub Due_within_21_days()
    Dim Date_To_Use
    Date_To_Use = Format((Date + 21), "dd/mm/yyyy")
    Columns("A:B").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=2, Criteria1:="<" & Date_To_Use, Operator:=xlAnd
    'Stop
    Columns("A:B").Select
    Selection.Copy
    Range("E1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.AutoFilter
End Sub

Many thanks,
Mark
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi There

Maybe this code will work for you dude

_/_/_/_/_/_/_/_/_/_/_/_/_/

Sub Due_within_21_days()
Dim Date_To_Use
Date_To_Use = Format((Date + 21), "dd/mm/yyyy")
MsgBox Date_To_Use
With Columns("A:B")
.Resize(, 1).Offset(, 1).NumberFormatLocal = "dd/mm/yyyy"
.AutoFilter Field:=2, Criteria1:="<" & Date_To_Use, Operator:=xlAnd
.Copy [E1]
.AutoFilter
End With
End Sub

_/_/_/_/_/_/_/_/_/_/_/_/_/

Let us know what happens please
 
Upvote 0
Gareth,
Thanks, Slight improvement.
The msgbox displays "07/04/2003" but the filter is still looking for < 04/07/2003.

Any further ideas?

Thanks,
Mark
 
Upvote 0
Coercing the date to a Long Integer worked for me:

Code:
Sub Test()
    Dim Rng As Range
    Dim Date_To_Use
    Date_To_Use = CLng(Date + 21)
    Set Rng = Range("A1:A" & Range("A1").End(xlDown).Row).Resize(, 2)
    With Rng
        .AutoFilter Field:=2, Criteria1:="<" & Date_To_Use, Operator:=xlAnd
        .Copy Range("E1")
        .AutoFilter
    End With
    Application.CutCopyMode = False
End Sub
 
Upvote 0
Hi there Again bro

Both my code and Andrews code works perfectly. . . Are you sure the date format you are using is all set to the same value's ?

:LOL:
 
Upvote 0

Forum statistics

Threads
1,221,709
Messages
6,161,431
Members
451,705
Latest member
Priti_190

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