Filter Date

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Trying to filter worksheet on date less than 6/5/2018 using the below code. Perplexed....


Thank you for any help.

Code:
Sub DateLessThan()Dim Lastrow As Long, i As Long
Dim ws As Worksheet
Set ws = ActiveSheet
Dim sdate As String
sdate = 6 / 5 / 2018
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
If ws.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
ws.Range("A1:E1").AutoFilter field:=1, Criteria1:="<=" & sdate




End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Maybe...

Code:
Sub DateLessThan()
Dim Lastrow As Long, i As Long
Dim ws As Worksheet
Set ws = ActiveSheet
[COLOR=#0000ff]Dim sdate As Long
sdate = DateSerial(2018, 6, 5) ' sdate = June 5, 2018
[/COLOR]Lastrow = Range("A" & Rows.Count).End(xlUp).Row
If ws.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
ws.Range("A1:E1").AutoFilter field:=1, Criteria1:="<=" & sdate
End Sub

M.
 
Upvote 0
If the dates in col A are real dates (numbers), try dimensioning sdate as a date.

Dim sdate As Date
sdate = #6/5/2018#
 
Upvote 0
Trying to apply Multiple Dates using the array
Code:
Sub FilterDateWithArray()Dim Lastrow As Long, i As Long
Dim ws As Worksheet
Set ws = ActiveSheet


Lastrow = Range("A" & Rows.Count).End(xlUp).Row
If ws.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
ws.Range("A1:E1").AutoFilter field:=1, Criteria1:=Array("6/5/2018", "6/14/2018", "5/5/2018")
End Sub
 
Upvote 0
An alternative is to use Advanced Filter with a formula as criteria - something like

Code:
Sub AdvFilter()
    'Range Mylist contains dates
    Dim Lastrow As Long, ws As Worksheet

    Set ws = ActiveSheet
    With ws
        'Clear filter
        .AutoFilterMode = False
        'Cell above criteria formula should be blank
        .Range("G1") = ""
        'Insert criteria (formula) in G2
        .Range("G2").Formula = "=ISNUMBER(MATCH(A2,MyList,0))"
        Lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("A1:E" & Lastrow).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
            .Range("G1:G2"), Unique:=False
    End With
End Sub

M.
 
Last edited:
Upvote 0
I would need to define array (Mylist) to Column A where the dates are located?

No. MyList is a named range that contains the dates that you want to be displayed after filter, for example (gray area)


[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
G
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD]
MyList​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD="bgcolor: #BFBFBF"]
06/05/2018​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD="bgcolor: #BFBFBF"]
06/14/2018​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD="bgcolor: #BFBFBF"]
05/05/2018​
[/TD]
[/TR]
</tbody>[/TABLE]

M.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,734
Messages
6,174,186
Members
452,550
Latest member
southernsquid2

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