Solved

Filter on Date and time

Posted on 2010-09-02
15
373 Views
Last Modified: 2012-05-10
Hello,

I have slightly tricky filter problem in Excel 2007.

I have a filter which filters on the previous days date.  For example, if today is 02/09/2010, the filter would show the data for 01/09/2010.  Unfortunately, the data imported have a time stamp included with the date.

I would like a filter which would filter from midnight at the start of the previous day until the end of the previous day.  

If the following filter was applied:
    ActiveSheet.Range("$A$1:$H$5000").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=Date - 1, Criteria2:=Time 00:00<=23:59

the data displayed would be:

01/09/2010 00:03
01/09/2010 19:00
01/09/2010 23:43

Thanks in advance.
0
Comment
Question by:infosec36
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 12

Expert Comment

by:tilsant
Comment Utility
Try this:

ActiveSheet.Range("$A$1:$H$5000").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, Date - 1)



Tils
0
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
ActiveSheet.Range("$A$1:$H$5000).AutoFilter Field:=1, Operator:=xlAnd, Criteria1:=">=" & Format(Date - 1, "yyyy-mm-dd"), Criteria2:="<" & Format(Date, "yyyy-mm-dd")
End Sub
0
 

Author Comment

by:infosec36
Comment Utility
Sorry guys,

I couldn't get either to work.

Tilsant, your solution did not show any records when the filter was applied.

peter57r, your solution had a compile error.  The message was "Expected: list seperator or )" on "yyyy-mm-dd".

Thanks
0
 
LVL 12

Expert Comment

by:tilsant
Comment Utility
Can u post a sample data file?
Is your data having Column A with date values?
Is there any date with yesterday's date??
0
 
LVL 77

Accepted Solution

by:
peter57r earned 150 total points
Comment Utility
Sorry there is a " missed in the address:

ActiveSheet.Range("$A$1:$H$5000").AutoFilter Field:=1, Operator:=xlAnd, Criteria1:=">=" & Format(Date - 1, "yyyy-mm-dd"), Criteria2:="<" & Format(Date, "yyyy-mm-dd")
0
 

Author Comment

by:infosec36
Comment Utility
Hello Tilsant,

Column A has data with date and time values and yes there is data with yesterday's date.  The following is contained in column A:

27/08/2010 07:02
27/08/2010 07:02
27/08/2010 07:02
27/08/2010 07:02
27/08/2010 07:02
27/08/2010 07:02
27/08/2010 07:02
27/08/2010 07:02
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:03
27/08/2010 07:04
27/08/2010 07:04
27/08/2010 07:04
27/08/2010 07:04
27/08/2010 07:04
27/08/2010 07:04
27/08/2010 07:04
27/08/2010 07:04
27/08/2010 07:04
31/08/2010 12:42
31/08/2010 12:42
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33
01/09/2010 09:33

Thanks.
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 total points
Comment Utility
Both of these seem to work.  I have not tried the other suggestions.
Sub test()

    

    With ActiveSheet.[a1]

        .AutoFilter

        .AutoFilter Field:=1, Criteria1:=xlFilterYesterday, Operator:=xlFilterDynamic

    End With

    

End Sub



Sub test2()

    

    With ActiveSheet.[a1]

        .AutoFilter

        .AutoFilter Field:=1, Criteria1:=">=" & Date - 1, Operator:=xlAnd, Criteria2:="<" & Date

    End With

    

End Sub

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Expert Comment

by:tilsant
Comment Utility
Try this one!

ActiveSheet.Range("$A$1:$H$5000").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, Format((Date - 1), "dd/mm/yyyy"))



Tils
0
 

Author Closing Comment

by:infosec36
Comment Utility
Thanks guys.

matthewspatrick, your first solution worked perfectly but your second returned no results.

peter57r, your solution worked perfectly and I have incorporated it into my macro.

Thanks for everyone's help.
0
 

Author Comment

by:infosec36
Comment Utility
Sorry tilsant,  I didn't spot your solution until I had closed the question.

0
 
LVL 12

Expert Comment

by:tilsant
Comment Utility
Did my last solution work for you??

I guess there was some conflict with the Date Format in VBA and Excel!
0
 
LVL 12

Expert Comment

by:tilsant
Comment Utility
And that might be the reason why matthewspatrick's 2nd solution didn't work for you!
0
 

Author Comment

by:infosec36
Comment Utility
Hello tilsant,

Unfortunately your solution did not return any records. I agree with your assessment regarding a possible conflict.

Thank you very much for your time and effort.
0
 
LVL 12

Expert Comment

by:tilsant
Comment Utility
Thats ok infosec36. Points aren't a big concern here for me!


Tils
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Almost certainly a formatting conflict :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now