+ Reply to Thread
Results 1 to 8 of 8

Autofilter by date range

  1. #1
    Registered User
    Join Date
    04-19-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Autofilter by date range

    Hi All,

    I'm completely new to VBA so please bear with me. I've been reading various sources for the past few hours on how to autofilter a table based on 2 variable dates. I've tried various different scripts but can't get it to work. Please could someone show me what I'm doing wrong:

    Background:
    I have a simple table of data with column 1 being a date input - the column headers are at B8:G8.
    At cell K5 I have a start date which is user entered
    At cell J5 I have an end date which is user entered
    I wish to filter the table for entries between these 2 dates.

    At the minute my code reads as:

    Sub FilterJB()

    Dim DateStart As Date
    Dim DateEnd As Date

    DateStart = [K5].Value
    DateEnd = [L5].Value

    ' Set the filter

    Range("B8:G8").Select
    Selection.AutoFilter

    ActiveSheet.Range("$B$8:$G$5005").AutoFilter Field:=2, Criteria1:="J5", Operator:=xlAnd
    ActiveSheet.Range("$B$8:$G$5005").AutoFilter Field:=1, Criteria1:=">=" & DateStart, Operator:=xlAnd, Criteria2:="<=" & DateEnd


    End Sub


    Any help would be much appreciated.

    Best wishes

    Joe

  2. #2
    Forum Contributor JP Romano's Avatar
    Join Date
    10-09-2008
    Location
    Princeton, NJ
    MS-Off Ver
    2010
    Posts
    500

    Re: Autofilter by date range

    It looks like you may have your cells mixed up (unless I'm reading something wrong).
    Start date = K5
    End date = L5

    I'd also use the variables you're setting:

    Please Login or Register  to view this content.
    Note that this only applies that filter to the first column. You could fairly easily amend it to apply to each individual column by changing the value after Activesheet.range...

    Good luck!
    Last edited by JP Romano; 04-19-2010 at 05:06 PM.

  3. #3
    Registered User
    Join Date
    04-19-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Autofilter by date range

    Hi thanks for the prompt response:

    I've tried the code you provided but I get 'runtime error 13, type mismatch' and the debug window highlights DateEnd = [L5].Value?

  4. #4
    Registered User
    Join Date
    04-19-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Autofilter by date range

    Ok I've been playing around a little more and currently have this code:

    Please Login or Register  to view this content.
    I forgot to mention that I'm also wanting to filter by a particular member field as well as the date range which is the first filter criteria.

    I'm confused as when I run the script, no entries are filtered but when I manually view the autofilter settings using the drop downs on each column, they are all set to the correct values so it's sort of working. As soon as I hit OK on any one of the filter settings, the results appear? It's almost as though it needs refreshing after the values are set?

  5. #5
    Forum Contributor JP Romano's Avatar
    Join Date
    10-09-2008
    Location
    Princeton, NJ
    MS-Off Ver
    2010
    Posts
    500

    Re: Autofilter by date range

    Hi... can you attach your file so I can work with the proper info? The code I sent you worked fine for me, but if I can see exactly what you have I may be able to help a bit better.
    Thanks Joe!

  6. #6
    Registered User
    Join Date
    04-19-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Autofilter by date range

    Quote Originally Posted by JP Romano View Post
    Hi... can you attach your file so I can work with the proper info? The code I sent you worked fine for me, but if I can see exactly what you have I may be able to help a bit better.
    Thanks Joe!
    Thanks for the offer to look at my spreadsheet which I've attached.

    To explain a little better, the spreadsheet is basically to record fuel usage by members of a fuel syndicate. The first tab is where each transaction will be recorded in a table and also where I'd like the autofilter to operate. I've set up a filter criteria area top right which is where the member will be selected along with a date range. I've managed to get the 'turn filter off' button to work!

    Thanks very much for your help.
    Attached Files Attached Files

  7. #7
    Forum Contributor JP Romano's Avatar
    Join Date
    10-09-2008
    Location
    Princeton, NJ
    MS-Off Ver
    2010
    Posts
    500

    Re: Autofilter by date range

    Okay, I see the issue.

    The dates are formatted differently. I modifed all the dates to use the same format:
    mm/dd/yy
    Then added a line to select the filter for the dates seperately and it worked like a charm.

    Please Login or Register  to view this content.
    Try putting your dates all in the same format, whatever it may be, then let me know how it goes.

    Good luck!

  8. #8
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Autofilter by date range

    Here's a link which explains how autofilter interprets date values: http://www.ozgrid.com/VBA/autofilter-vba-dates.htm
    I haven't worked through an example myself but it may explain why it works for JP (USA) but not Joeb in the UK.

    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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