Solved

Auto Filter on <Today - 7 Days> Macro in Excel

Posted on 2003-10-28
7
1,144 Views
Last Modified: 2012-06-21
I have the following macro set up in Excel and I'd like to make the date that I autofilter on be dynamic so the date range is: >= today - (7days)

Any suggestions?


Sub Weekly_Status()
'
' Weekly_Status Macro
' Macro recorded 10/28/2003 by xxxxxxxxxxxxxxxx
'

'
    Rows("1:1").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=2, Criteria1:="<>N/A", Operator:=xlAnd
    ActiveWindow.SmallScroll ToRight:=3
    Columns("G:I").Select
    Selection.NumberFormat = "mm/dd/yyyy;@"
    Selection.AutoFilter Field:=8, Criteria1:=">=10/21/2003", Operator:=xlOr _
        , Criteria2:="="
    ActiveWindow.SmallScroll Down:=-6
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.LargeScroll ToRight:=-1
End Sub
0
Comment
Question by:Perogi
[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
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:ampapa
ID: 9637666
Give this a try...

Sub Weekly_Status()
'
' Weekly_Status Macro
' Macro recorded 10/28/2003 by xxxxxxxxxxxxxxxx
'

dim todaylessseven as string

todaylessseven = ">=" &date
'
    Rows("1:1").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=2, Criteria1:="<>N/A", Operator:=xlAnd
    ActiveWindow.SmallScroll ToRight:=3
    Columns("G:I").Select
    Selection.NumberFormat = "mm/dd/yyyy;@"
    Selection.AutoFilter Field:=8, Criteria1:=todaylessseven, Operator:=xlOr _
        , Criteria2:="="
    ActiveWindow.SmallScroll Down:=-6
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.LargeScroll ToRight:=-1
End Sub
0
 
LVL 8

Expert Comment

by:ampapa
ID: 9637674
Sorry the date filed was missing a space.

it should be - todaylessseven = ">=" & date
0
 

Author Comment

by:Perogi
ID: 9639177
okay, but your answer is just >= the current date.  I need it to be the current date minus seven days.
0
Industry Leaders: 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 8

Expert Comment

by:ampapa
ID: 9640696
Sorry If orgot to subtract 7. This should do it.

Sub Weekly_Status()
'
' Weekly_Status Macro
' Macro recorded 10/28/2003 by xxxxxxxxxxxxxxxx
'

dim todaylessseven as string

todaylessseven = ">=" & date -7
'
    Rows("1:1").Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=2, Criteria1:="<>N/A", Operator:=xlAnd
    ActiveWindow.SmallScroll ToRight:=3
    Columns("G:I").Select
    Selection.NumberFormat = "mm/dd/yyyy;@"
    Selection.AutoFilter Field:=8, Criteria1:=todaylessseven, Operator:=xlOr _
        , Criteria2:="="
    ActiveWindow.SmallScroll Down:=-6
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.LargeScroll ToRight:=-1
End Sub
0
 

Author Comment

by:Perogi
ID: 9642549
I thought the date calculation would be difficult you need to specify to subtract 7 days and not 7 years or 7 months.  Anyway, the above solution did not work.  I get the following error when running the macro:

"Autofilter method of Range class failed"

The debugger brings me to the line starting with: "Selection.AutoFilter Field:=8,"....
0
 
LVL 8

Accepted Solution

by:
ampapa earned 150 total points
ID: 9646790
I was basing my answer on what you had supplied. Sorry I was taking a short cut. I just ran this macro and it worked fine for me with date data in cell G1:G40. The date calculation is as easy as it appears.

Sub Weekly_Status()

Dim todaylessseven As String
MsgBox Date
MsgBox Date - 7

todaylessseven = ">=" & Date - 8
    Range("G1:G40").Select
    Selection.AutoFilter Field:=1, Criteria1:=todaylessseven, Operator:=xlOr _
        , Criteria2:="="

End Sub

ampapa,



0
 

Author Comment

by:Perogi
ID: 9673681
Awesome, it works now.  I just needed to add a test to see if autofilter was already on.  That is where I was getting my error message.  I was attempting to turn autofilter on on something that already was autofiltered.  Thanks for the help!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

733 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