Solved

Filter on Date and time

Posted on 2010-09-02
15
379 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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 150 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 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 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 92

Expert Comment

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

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

821 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