?
Solved

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

Posted on 2003-10-28
7
Medium Priority
?
1,230 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 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

743 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