VBA - Filter between two dates

Mr Stanford

Board Regular
Joined
Sep 12, 2009
Messages
142
Can anyone help on the following query?

I have taken data from a gantt chart in microsoft project and am looking to filter between dates by dropdown boxes and a macro button, this is to show all of the activities that are happening between the selected dates.

I have the start date in column 6 and the finish date in column 7.

I would like the user to select a start date and an end date and any task name item that occurs between those dates are shown.

Currently I have the code as shown below, but I know it is wrong as it filters the start date, then filters the start date results and add the finish date filter to the existing filter.

wt7ok4.png


2i6dnyx.png


Code:
Sub Between()
Sheets("Sheet1").Select
If Sheets("Sheet1").AutoFilterMode Then Cells.AutoFilter


    
    
 Dim lDateFrom As Long
    Dim lDateTo As Long
     
    If Range("Sheet6!L14") > Range("Sheet6!L16") Then
        lDateTo = CDbl(Range("Sheet6!L14"))
        lDateFrom = CDbl(Range("Sheet6!L16"))
    Else
        lDateTo = CDate(Range("Sheet6!L16"))
        lDateFrom = CDate(Range("Sheet6!L14"))
    End If


     
    With Sheets("Sheet1")
        Range("$A$4:$Q$220").AutoFilter Field:=6, Criteria1:=">=" & lDateFrom, Operator:=xlAnd, Criteria2:="<=" & lDateTo, _
        Operator:=xlAnd
        Range("$A$4:$Q$220").AutoFilter Field:=7, Criteria1:=">=" & lDateFrom, Operator:=xlAnd, Criteria2:="<=" & lDateTo
    End With
    
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Not sure you could do that with an autofilter unless you employed a slave column. Whether each line is displayed is not dependant on one cell but two.
 
Upvote 0
Now thinking about it im trying to think that if start date (data) is less than end date (filter criteria) and if start date (filter criteria) is less than end date (data) then they overlap....
 
Upvote 0
I have a filter on project that is set up as follows;

Finish is greater than or equal to "start or finish after start date"
and Start is less "end date".

But not sure how to do that with filters in excel.
 
Upvote 0
Try filtering the start date column with the criteria less than end of February. Then filter the end date column with the criteria greater than the 1st of January.
 
Upvote 0
It's doable. But beware:- filtering under date can need some special formatting of the date (particularly if you aren't Ummerican)
 
Upvote 0
Suppose the Filter dates are 1-April-2016 through 30-Jun-2016

What tasks are you wanting to see?
Anything that:
- starts before 30-Jun AND (doesn't end OR ends after 1-April) {call these tests A, B, C)

2-Jan to 13-Mar fails C
2-Jan to 13-Apr Pass
2-Jan to 13-Jul Pass
2-Jan to Unfunished Pass

2-Apr to 13-Apr Pass
2-Apr to 13-Jul Pass
2-Apr to Unfinished Pass

2-Jul to 13-Jul fails A
2-Jul to Unfinished fails A
 
Upvote 0
Code:
With Sheets("Sheet1") 
     Range("$A$4:$Q$220").AutoFilter Field:=6, Criteria1:="<=" & lDateTo
     Range("$A$4:$Q$220").AutoFilter Field:=7, Criteria1:=">=" & lDateFrom, Operator:=xlOr, Criteria2:="="
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,218,240
Messages
6,141,340
Members
450,352
Latest member
lohpa

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