VBA Filter Data to Date

Andrew_UK

Board Regular
Joined
Jun 3, 2015
Messages
53
Hi All,

Part of my code isn't working. I need to filter the data to a date specified by the user.

Code:
Sub Pomotions()

Dim Lastrow as Integer
Dim Daterange As Date

lastrow = Columns("A:A").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row

Daterange = InputBox("Please enter effective date (DD/MM/YYYY)")

Rows("1:1").Select
Selection.AutoFilter

ActiveSheet.Range("$A$1:$BL" & lastrow).AutoFilter Field:=4, Operator:= _
        xlFilterValues, Criteria1:=Daterange
End Sub

I can see dates in my data which match the exact date range that I input, but still it skips them.

I've built many filters before, but never tried to do this on date filters. Any ideas where I'm going wrong?

I tried recording macros using the filter "Date Filter", but couldn't see any nuances that jumped out at me.

Thanks in advance
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I believe that I read that VBA uses US Date formatting.

So test out entering the date in "MM/DD/YYYY" format and see if that works for you.
If it does, then there are some options you can pursue:
- enter the date in US Format
- use a Date Picker
- declare "Daterange" as a string and convert it to a date in VBA before applying to your filter
 
Upvote 0
See if this works for you:
Code:
Sub Pomotions()
    Dim Lastrow As Long
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
    Dim Daterange As String
    Daterange = InputBox("Please enter effective date (DD/MM/YYYY)")
    ActiveSheet.Range("A1:BL" & Lastrow).AutoFilter Field:=4, Criteria1:=CDate(Daterange)
End Sub
 
Upvote 0
This is how I normally do it
Code:
Criteria1:=Format(Daterange, "[COLOR=#ff0000]dd/mm/yyyy[/COLOR]")
The part in red need to be the same format as the cells
 
Upvote 0
I believe that I read that VBA uses US Date formatting.

So test out entering the date in "MM/DD/YYYY" format and see if that works for you.
If it does, then there are some options you can pursue:
- enter the date in US Format
- use a Date Picker
- declare "Daterange" as a string and convert it to a date in VBA before applying to your filter

Hi Joe, thanks for your reply. I tried the US date format and had no luck.

Also I tested using the macro recorder and then clicking "Date Filters", "Equals to"... to see how it recorded the process and here's what I got;

Code:
Sub Macro1()

    Rows("1:1").Select
    Selection.AutoFilter
    Range("D1").Select
    ActiveSheet.Range("$A$1:$BF$21059").AutoFilter Field:=4, Criteria1:= _
        "=09/11/2017", Operator:=xlAnd
End Sub

This returned data for the 9th of November as intended, seeming to imply that US/UK date formats aren't the issue.

Alternatively, If I try typing it in as a search string into the box within the filter I get it saying no results before returning the data.

https://ibb.co/ev5Xnb
ev5Xnb

ev5Xnb

ev5Xnb


I worry that this is the process which my original Macro was attempting to apply and therefore getting no results.
 
Last edited:
Upvote 0
Hi Fluff,

Thanks for your reply. I tried that, I can see the logic in what your code says. But I couldn't seem to get it to work for me.

Interestingly, despite
Code:
Dim Daterange as Date
and despite
Code:
[COLOR=#333333]Format(Daterange, "[/COLOR][COLOR=#ff0000]dd/mm/yyyy[/COLOR][COLOR=#333333]")[/COLOR]
, when I look back at the results it still seems to have applied an erroneous text filter instead of a date filter.

I love VBA at times, but at other times it infuriates me :laugh:
 
Upvote 0
This works for me
Code:
Sub Pomotions()

Dim Lastrow As Integer
Dim Daterange As Date

Lastrow = Columns("A:A").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row

Daterange = InputBox("Please enter effective date (DD/MM/YYYY)")

'Rows("1:1").Select
Rows(1).AutoFilter

ActiveSheet.Range("$A$1:$BL" & Lastrow).AutoFilter Field:=4, Operator:= _
        xlFilterValues, Criteria1:=Format(Daterange, "DD/MM/YYYY")
End Sub
What does your date look like in the cell & how does it appear in the formula bar?
 
Upvote 0
It sounds like maybe either:
- your date entry are actually entered as text and not dates (can easily be confirmed with the ISNUMBER function, as dates are stored as numbers in Excel)
- there is a time component in your dates that is not being accounted for
 
Upvote 0
Thanks for all your help guys,

I've got this working now!!
Glad to hear it!

Out of curiosity, what was the issue?
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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