[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Filter on Date and time

Posted on 2010-09-02
15
Medium Priority
?
384 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

834 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