Solved

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

Posted on 2003-10-28
7
1,086 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now