?
Solved

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

Posted on 2003-10-28
7
Medium Priority
?
1,339 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
  • 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 450 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 In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

809 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