Consulting

Results 1 to 7 of 7

Thread: Solved: Filtering records older than one year ago

  1. #1
    VBAX Regular
    Joined
    Apr 2008
    Posts
    97
    Location

    Solved: Filtering records older than one year ago

    I got the code down to use the autofilter but am having problems with the date format for the filter.

    There must be a one liner to give me a date in the MM/DD/YYYY format like Filterdate=today()-365
    This is what I got so far

    [vba]

    Dim curyear As Date
    curyear = today() - 365
    Format(curyear, "mm/dd/yyyy")
    Workbooks.Open Filename:="C:\BDR\33activity.xls"
    Application.Goto Reference:="data"
    Selection.AutoFilter
    Selection.AutoFilter Field:=5, Criteria1:=curyear, Operator:=xlAnd
    [/vba]

    Just having one of those days again!
    Last edited by Bob Phillips; 09-24-2009 at 10:58 AM. Reason: Added VBA tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I tend to use code like this

    [vba]

    Dim curyear As Date
    curyear = today() - 365
    Workbooks.Open Filename:="C:\BDR\33activity.xls"
    Application.Goto Reference:="data"
    Selection.AutoFilter
    Selection.AutoFilter Field:=5, Criteria1:=Format(curyear, Selection.Cells(2,1).NumberFormat) , Operator:=xlAnd
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Apr 2008
    Posts
    97
    Location
    The Today() line comes up as function not defined.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Didn't notice that, use Datae not Today() in VBA.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Apr 2008
    Posts
    97
    Location
    Sorry, it does not return any data.
    Would it be because of the time in the data's date field?

    I would like to filter only records that are equal to or greater than one year ago essentially providing a rolling year of data.

    I attached one of the data files with the actual data if this helps.

    Thanks again, and again, and again

    This is the code so far,

    [VBA]
    Sub Macro4()
    Dim curyear As Date
    curyear = Date - 365
    Workbooks.Open Filename:="C:\BDR\33activity.xls"
    Application.Goto Reference:="data"
    Selection.AutoFilter
    Selection.AutoFilter Field:=5, Criteria1:=Format(curyear, Selection.Cells(2, 1).NumberFormat), Operator:=xlAnd
    Selection.Copy
    End Sub
    [/VBA]</IMG></IMG></IMG></IMG>

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub Macro4()
    Dim curyear As Date
    curyear = Date - 365
    Workbooks.Open Filename:="C:\BDR\33activity.xls"
    Application.Goto Reference:="data"
    Range("data").AutoFilter Field:=5, Criteria1:=">" & CLng(curyear)
    Range("data").Copy
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Apr 2008
    Posts
    97
    Location

    Solved, filtering records older than one year.

    Once again.
    Thanks!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •