Filter column by Date Range produces no results

rogerbowering

New Member
Joined
Apr 29, 2013
Messages
14
I want the user of a worksheet to be able to select two dates and for the worksheet to display all rows where (column 9) falls between (or equal to those two dates)

I've recorded a macro and used code from another site and the problem I have is, when I run the code or the macro, the sheet status displays 0 of 10392 records found. The column that is being filtered shows the little filter icon. If I click on the filter icon for the column I see 'Text Filters' has been selected and 'Custom Filter'. When I click on 'Custom Filter' I can see the two dates I chose in the >= box and <= box and when I click on the OK button my sheet now displays the 1397 rows I expected to see

Why doesn't the VBA or running the macro complete the job and leave the filter 'unfinished' .

I'm using:
Dim StartDate As Date
Dim EndDate As Date
StartDate =InputBox("Enter Date From","Select Start Date")
EndDate=InputBox("Enter Date To","Select End Date")
ActiveSheet.Range("$A$1:$R$10393").AutoFilter Field:=9, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<="&EndDate
Sheets("sheetname").Activate


I've noticed that the display I get when I run the macro shows my header/filter row and then rows 10394 onward
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
PS As an aside it would be nice to have an Input box with both dates rather than two separate Input Boxes, but that's another thread
 
Upvote 0
Dates & Autofilter can be a bit of a problem.
Try passing your dates to long variable using dateserial something like this:

Code:
Sub DateFilter()
Dim StartDate As Variant
Dim EndDate As Variant
Dim lDateFrom As Long
Dim lDateTo As Long
Datefrom:
    StartDate = InputBox("Enter Date From", "Select Start Date")
    If StartDate = vbNullString Then Exit Sub
    If Not IsDate(StartDate) Then MsgBox "Not A Valid Date": GoTo Datefrom
DateTo:
    EndDate = InputBox("Enter Date To", "Select End Date")
    If EndDate = vbNullString Then Exit Sub
    If Not IsDate(EndDate) Then MsgBox "Not A Valid Date": GoTo DateTo
    
    lDateFrom = DateSerial(Year(StartDate), Month(StartDate), Day(StartDate))
    lDateTo = DateSerial(Year(EndDate), Month(EndDate), Day(EndDate))
    
    With ActiveSheet
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("$A$1:$R$10393").AutoFilter Field:=9, _
                                                  Criteria1:=">=" & lDateFrom, _
                                                  Operator:=xlAnd, _
                                                  Criteria2:="<=" & lDateTo
    End With
End Sub

Dave
 
Last edited:
Upvote 0
Brilliant, thank you


Dates & Autofilter can be a bit of a problem.
Try passing your dates to long variable using dateserial something like this:

Code:
Sub DateFilter()
Dim StartDate As Variant
Dim EndDate As Variant
Dim lDateFrom As Long
Dim lDateTo As Long
Datefrom:
    StartDate = InputBox("Enter Date From", "Select Start Date")
    If StartDate = vbNullString Then Exit Sub
    If Not IsDate(StartDate) Then MsgBox "Not A Valid Date": GoTo Datefrom
DateTo:
    EndDate = InputBox("Enter Date To", "Select End Date")
    If EndDate = vbNullString Then Exit Sub
    If Not IsDate(EndDate) Then MsgBox "Not A Valid Date": GoTo DateTo
    
    lDateFrom = DateSerial(Year(StartDate), Month(StartDate), Day(StartDate))
    lDateTo = DateSerial(Year(EndDate), Month(EndDate), Day(EndDate))
    
    With ActiveSheet
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("$A$1:$R$10393").AutoFilter Field:=9, _
                                                  Criteria1:=">=" & lDateFrom, _
                                                  Operator:=xlAnd, _
                                                  Criteria2:="<=" & lDateTo
    End With
End Sub

Dave[/QUOTE]
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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