VBA Filter by Dates

Simondo

New Member
Joined
Jun 10, 2014
Messages
13
Hi All,

I have a spreadsheet with a list of employee names, in the adjacent columns are a start date, and end date.

I want to filter the start date by "before or equal to" today
and the end date by "after or equal to" today

I have been trying non stop, so far I have the following code, which will only return the dates if they equal today's date.

Code:
Sub Dates()

Dim DateToday As Date
DateToday = Worksheets("Date").Range("A1").Value


    Worksheets("Histogram").ListObjects("Table_owssvr_1").Range.AutoFilter Field:=5, _
        Criteria1:=Array(2, DateToday), Operator:=xlAnd
    Worksheets("Histogram").ListObjects("Table_owssvr_1").Range.AutoFilter Field:=6, _
        Criteria1:=Array(2, DateToday), Operator:=xlAnd
        
End Sub

I'm not sure how to set it to filter for any dates before or after.

Any help greatly appreciated

Regards,
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try it like this instead:

Code:
Sub FilterDates()
  
  Dim Today As Date
  Today = Worksheets("Date").Range("A1").Value
  
  With Worksheets("Histogram").ListObjects("Table_owssvr_1").Range
    .AutoFilter Field:=5, Criteria1:="<=" & CLng(Today)
    .AutoFilter Field:=6, Criteria1:=">=" & CLng(Today)
  End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,202,981
Messages
6,052,900
Members
444,610
Latest member
dodong

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