+ Reply to Thread
Results 1 to 4 of 4

Filtering A Range Of Dates

  1. #1
    Registered User
    Join Date
    12-15-2015
    Location
    Regina, Saskatchewan, Canada
    MS-Off Ver
    2013
    Posts
    22

    Filtering A Range Of Dates

    Good Morning All,

    I am trying to use the below code to filter out a range of dates. I would like to have it so I can filter out the current year, last year then 3 years ago. Which is set up in tab "Control" But when I set these Value's and the Macro gets to the filter portion it gives me the error "Run-time error '1004': AutoFilter method of Range class failed.

    Any help on this would be most appreciated.
    Thank you.


    Sub Macro1()
    '
    ' Macro1 Macro
    '
    Dim endRow As Long
    Dim rangeOne As Range, rangeTwo As Range

    '
    Sheets.Add.Name = "Control"
    Sheets("CONTROL").Range("A1") = "CURRENT DATE:"
    Sheets("CONTROL").Range("B1") = Date
    Sheets("CONTROL").Range("B2").Value = "=DATE(YEAR(B1)-1,MONTH(B1),DAY(B1))"
    Sheets("CONTROL").Range("B3").Value = "=DATE(YEAR(B1)-2,MONTH(B1),DAY(B1))"
    Sheets("CONTROL").Range("B4").Value = "=DATE(YEAR(B1)-3,MONTH(B1),DAY(B1))"

    Year0 = Sheets("CONTROL").Range("B1").Value
    Year1 = Sheets("CONTROL").Range("B2").Value
    Year2 = Sheets("CONTROL").Range("B3").Value
    Year3 = Sheets("CONTROL").Range("B4").Value

    Sheets("Report").Select
    endRow = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row
    ActiveSheet.Range("$A$1:$N$" & endRow).AutoFilter Field:=3, Criteria1:=">=" & Year0, Operator:=x1And, Criteria2:="<=" & Year1

    End Sub
    Last edited by M7A0S8T8E5R; 06-21-2016 at 11:49 AM.

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Filtering A Range Of Dates

    Do you get the same error if you change the red 1 to a l (lower case L)
    Year0, Operator:=x1And, Criteria2:="<=" & Year1
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Registered User
    Join Date
    12-15-2015
    Location
    Regina, Saskatchewan, Canada
    MS-Off Ver
    2013
    Posts
    22

    Re: Filtering A Range Of Dates

    Thank you for your help! That was exactly it, keen eye you have there!

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Filtering A Range Of Dates

    Just a tip but if you had declared all your variables and had Option Explicit at the top of your code then it would have shown you where the issue was without the need of a "keen eye" as x1And would have come up as an undefined variable.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2013, 10:30 AM
  2. Filtering dates VBA
    By Bishonen in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-17-2013, 09:09 AM
  3. Filtering Dates (need all dates within the next 30 days)
    By debesh_b in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-22-2008, 10:27 PM
  4. [SOLVED] Excel auto filtering to find a range of dates in a list
    By Candy in forum Excel General
    Replies: 2
    Last Post: 01-31-2006, 10:35 AM
  5. [SOLVED] filtering dates
    By Peo Sjoblom in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 04:05 AM
  6. filtering dates
    By [email protected] in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  7. filtering dates
    By [email protected] in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  8. [SOLVED] filtering dates
    By [email protected] in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-20-2005, 01:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1