+ Reply to Thread
Results 1 to 12 of 12

AutoFilter Date Criteria

  1. #1
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    AutoFilter Date Criteria

    Hello,

    I'm trying to filter the AllData from the Summary sheet, so I have code which runs when a total is double-clicked on the Summary sheet. The trouble I'm having is specifying the date criteria for the AutoFilter. I'm getting a 1004 Runtime error in the attached. I'm trying to filter for a particular Area for the specific timeframes, in which the values (28, 14 etc) are fed from the currently active column.

    Can anyone advise on how to properly create the date criteria?

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,916

    Re: AutoFilter Date Criteria

    Hi, I do not know what your criteria are but you have forgotten to define the worksheet you're clicking

    Please Login or Register  to view this content.
    The bold text is what you were missing

    The bold red text is incorrect, you are addressing a column and this should be a row, but the criteria, well you can continue with it, the macro error is not there anymore

    The two Autofilter lines are to clear the previous autofilter, You have AutoFilter.False and that's wrong
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: AutoFilter Date Criteria

    I'm having a hard time trying to figure out what it is you want to do.

    First of all, it's a good idea to start every module off with Option Explicit. This will force you to define all your variables. So when you misspell one, the editor will let you know.

    For example Column is not defined so it is nothing and the code will fail there even if everything else was correct.

    Next, this macro will fire if you double click anywhere on the sheet. You need to limit it to just the range of interest. I am going to assume that these are cells B4:D8. So add this code in right after the declaration:

    If Intersect (Target, Range("$B$4:$D$8") ) is Nothing then Exit Sub

    This line of code checks to see if the double-clicked cell is in the range of interest and if it isn't (is nothing) then exit the subroutine.

    I am going to disagree with Hans about having to explicitly declare the sheet in this instance. It is almost always a good idea to explicitly state where you think you are in the workbook and for that matter which workbook you are in.

    However, there are times when this is not necessary. If you only have one workbook open, then obviously it is the active workbook.

    If you launch a macro from a button on a sheet, then you can safely assume that the sheet is the active sheet.

    Likewise any code that gets launched from a sheet rather than a module such as a double-click event, then it is also safe to assume that the sheet is the active sheet.

    However, Hans is on the right track. You do make reference to another sheet and his advice is valid except that he was pointing at the wrong sheet. I would modify his and your code thusly:

    Please Login or Register  to view this content.
    There is no need to select something to work on it. Generally speaking, in code, when you see:
    Please Login or Register  to view this content.
    You can replace it with:
    Please Login or Register  to view this content.
    As for the rest of the code, I can’t figure out what you are doing. How do the 4 Weeks, 3 Weeks and 2 Weeks relate to the dates? What is it that you want to filter? Do you want to filter on the Area name on the row with the double-clicked cell?
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  4. #4
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: AutoFilter Date Criteria

    Thank you both for your help.

    Hans, yes I had the Row/Column mixed up, the code I have now is:

    Please Login or Register  to view this content.
    dflak, in terms of what I want the date critera to do, when a value is double-clicked, I'd like the rows that comprise that value to be filtered in the AllData sheet. So when a TEST1 value is double-clicked and is in the 4-Week column, I want to see the AREA filtered for TEST1 and the Date column filtered for rows where the date is in 4 weeks. So I was trying to specify the criteria for the date as 'less than Today's Date +28 AND greater then Todays Date +21' as:

    Please Login or Register  to view this content.
    where Date is today's date and the ws.Cells(2, Column) is specifying the relevant numbers for that column's date criteria.

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,916

    Re: AutoFilter Date Criteria

    I imagined that much.
    And is it working now?

  6. #6
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: AutoFilter Date Criteria

    Quote Originally Posted by Keebellah View Post
    I imagined that much.
    And is it working now?

    No, getting a:

    Run-time error '1004'
    Application-defined or object-defined error

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,916

    Re: AutoFilter Date Criteria

    True, you're not using the row variable, and column is unknown or incorrect

    Please Login or Register  to view this content.
    I get an empty filter but maybe it works on your file with your date settings

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,916

    Re: AutoFilter Date Criteria

    Could you explain a little more of what your really trying to achieve?
    You criteria are all wrong.
    Add a Debug.Print in you macro so you can see what you are filtering.

  9. #9
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: AutoFilter Date Criteria

    Quote Originally Posted by Keebellah View Post
    Could you explain a little more of what your really trying to achieve?
    You criteria are all wrong.
    Add a Debug.Print in you macro so you can see what you are filtering.
    I've got it working, thanks for your help. I sorted out the criteria and adjusted the code to allow for the American Date format which had me puzzled for a while!

    Its just a dynamic filter which shows which rows in the AllData sheet make up the totals in the Orange area of the Summary sheet. So when you double-click a total cell, you can see which records match for the Area and Time Period. In reality, the AllData sheet will have
    many more columns of data.
    Attached Files Attached Files

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,916

    Re: AutoFilter Date Criteria

    Nice. Looks good and you restricted the double-click area. I was going to suggest that.

  11. #11
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: AutoFilter Date Criteria

    Quote Originally Posted by Keebellah View Post
    Nice. Looks good and you restricted the double-click area. I was going to suggest that.
    It's getting there, lots to do still - but delighted to have the Filter working.

    Thanks again for your help

  12. #12
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,916

    Re: AutoFilter Date Criteria

    You're welcome

+ 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. Multiple Autofilter Date Criteria on Userform using DSUM arguments
    By Ambassador777 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-28-2015, 03:38 PM
  2. Adding date criteria from text box to autofilter code
    By HeyInKy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-10-2014, 04:26 PM
  3. Problems with Autofilter, date as criteria which are in tow colomns
    By atkumar123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-06-2013, 06:35 AM
  4. Replies: 4
    Last Post: 03-16-2013, 08:33 AM
  5. Replies: 2
    Last Post: 07-06-2012, 11:42 AM
  6. Date criteria in autofilter
    By bramcode in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-22-2012, 06:10 AM
  7. autofilter criteria date
    By vsantoro in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-17-2010, 02:13 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