?
Solved

Filter on Date and time

Posted on 2010-09-02
15
Medium Priority
?
382 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:Sonia Bowditch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
15 Comments
 
LVL 12

Expert Comment

by:tilsant
ID: 33584780
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
ID: 33584796
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:Sonia Bowditch
ID: 33584877
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:tilsant
ID: 33584897
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 600 total points
ID: 33584925
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:Sonia Bowditch
ID: 33585346
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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 400 total points
ID: 33585424
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
 
LVL 12

Expert Comment

by:tilsant
ID: 33585597
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:Sonia Bowditch
ID: 33585599
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:Sonia Bowditch
ID: 33585614
Sorry tilsant,  I didn't spot your solution until I had closed the question.

0
 
LVL 12

Expert Comment

by:tilsant
ID: 33585630
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
ID: 33585637
And that might be the reason why matthewspatrick's 2nd solution didn't work for you!
0
 

Author Comment

by:Sonia Bowditch
ID: 33585649
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
ID: 33585694
Thats ok infosec36. Points aren't a big concern here for me!


Tils
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33587012
Almost certainly a formatting conflict :)
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

777 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