Filter between two dates problem

Kentetsu

Well-known Member
Joined
Jan 22, 2004
Messages
520
Hello,

I'm having some trouble determining what exactly the issue is here. For some reason, what is returned is only items related to the first date entered, rather than giving everything between the two dates.

Here is the code that performs the filter action:
Code:
.Range("A1:H80000").AutoFilter Field:=1, _
        Criteria1:=">=" & Date1, _
        Operator:=xlAnd, _
        Criteria2:="<=" & Date2



Here is the complete code for this section of the program:
Code:
Option Explicit

Sub GrabData()
Dim Target1 As String
Dim Date1 As Date
Dim Date2 As Date
Dim Div As String


Target1 = "J:\BBS Tracking\BBSLog.xlsm"
Date1 = Sheets("BBS Reporting").Range("D22") '.Value
Date2 = Sheets("BBS Reporting").Range("D23") '.Value
Div = Sheets("BBS Reporting").Range("D4").Value

Workbooks.Open Target1, ReadOnly:=True
            
    With Worksheets("Log")
        .Activate
        .Unprotect Password:="*******"
       
        .Range("A1:H80000").AutoFilter Field:=1, _
        Criteria1:=">=" & Date1, _
        Operator:=xlAnd, _
        Criteria2:="<=" & Date2
        
        .Range("A1:H80000").AutoFilter Field:=2, _
        Criteria1:=Div
        
        Worksheets.Add.Name = "Summary"
        
        Sheets("Log").Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
        
        Sheets("Summary").Activate
        
        ActiveSheet.Paste
        
        Application.DisplayAlerts = False
        
        Sheets("Log").Delete
        Sheets("Sheet2").Delete
        Sheets("Sheet3").Delete
        Application.DisplayAlerts = True
    End With
        
        With Sheets("Summary")
            .Columns("A:H").ColumnWidth = 14
            .Range("A1:H500").Select
    With Selection
        .HorizontalAlignment = xlCenter
    End With
                
        End With

Any thoughts are appreciated...
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Correction. When it Filters the data, I get everything within the date range except for the ending date.
So, if I'm filtering 10/9/15 - 10/15/15 (data for every date) the return I get would include all data from 10/9/15 - 10/14/15.

Very odd...

Okay, getting even stranger. I just tried manually filtering the date range, and get the same results using "filter between dates".
 
Last edited:
Upvote 0
I get everything within the date range except for the ending date.

That probably means your dates in the range actually contain a TIME Value as well
So if you're doing <= 10/14/2015
But a cell is say 10/14/2015 7:30:00
That is NOT less or equal to.

Try adding 1 to the End date, but use < instead of <=

Code:
.Range("A1:H80000").AutoFilter Field:=1, _
        Criteria1:=">=" & Date1, _
        Operator:=xlAnd, _
        Criteria2:="<" & Date2 + 1
 
Upvote 0
Once again you have saved me from a concussion my friend. :)

That was indeed the problem, and your response did correct the issue. Thank you very much for your time, and for yet another lesson in Excel.
 
Upvote 0
Jonmo1,

I overlooked this part of your advice: but use < instead of <=

It worked the way I had it (<=) but not when I removed the < from the equation. FYI.
 
Upvote 0

Forum statistics

Threads
1,215,852
Messages
6,127,322
Members
449,374
Latest member
analystvar

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