+ Reply to Thread
Results 1 to 7 of 7

Help on Autofilter operator

  1. #1
    Registered User
    Join Date
    06-24-2008
    Posts
    5

    Help on Autofilter operator

    I have a spreadsheet that I'm looking to filter on two criteria: Date created and date changed, the format for Date created is YYYYMM

    This is the code I'm trying to use:
    Dim LastCreated As String
    LastCreated = Application.InputBox("Enter required 'created month' in format YYYYMM")

    'filter on change date = blank and created date = LastCreated

    With Worksheets("Filtered").Range("A1")

    .AutoFilter field:=26, Criteria1:=""
    .AutoFilter field:=5, Criteria1:=">=" & LastCreated

    End With
    The issue I have is that this works fine as long as the second criterion is "=", but if I change it to ">=" then everything is filtered out even though there are entries that are definitely >= LastCreated, but I don't know what I'm doing wrong, or why I'm getting this behaviour. Can anyone give me any pointers on how this is working/why it's not working as I intended?

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,386

    Re: Help on Autofilter operator

    Try:

    (you should use complete date format as well): 12-29-2021

    Please Login or Register  to view this content.
    Last edited by JEC.; 12-29-2021 at 03:18 PM.

  3. #3
    Registered User
    Join Date
    06-24-2008
    Posts
    5

    Re: Help on Autofilter operator

    Tried this but no joy. I had a feeling it wouldn't work as I'd previously tried:
    'prompt for date of last report
    Dim LastCreated As String
    LastCreated = Application.InputBox("Enter required 'created month' in format YYYYMM")

    'convert LastCreated to Long
    Dim LastCreatedLong As Long
    LastCreatedLong = CLng(LastCreated)

    'do filter on change date

    With Worksheets("Filtered").Range("A1")

    .AutoFilter field:=26, Criteria1:=""
    .AutoFilter field:=5, Criteria1:=">=" & LastCreatedLong

    End With
    There's obviously something going on I don't understand.

    One other question - why would I need the full date format? If I want anything created in September 2021 or beyond then if the table says '202109' I can't understand why filtering for >=202109 doesn't work. Weirdly if I just want =202109 it works fine.

  4. #4
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,386

    Re: Help on Autofilter operator

    Well try to include the first of the month then, like 9-1-2021 and run this code. That should be working

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-24-2008
    Posts
    5
    Quote Originally Posted by JEC. View Post
    Well try to include the first of the month then, like 9-1-2021 and run this code. That should be working

    Please Login or Register  to view this content.
    OK, I'll give it a go. But can anyone explain what is going on? What if this had just been a number and not a date? How would I generalise this to any data type?

  6. #6
    Registered User
    Join Date
    06-24-2008
    Posts
    5

    Re: Help on Autofilter operator

    fixed it by changing the created date column to a number using this code:

    With Range("E2:E" & Cells(Rows.Count, 1).End(xlUp).Row)
    .NumberFormat = "General"
    .Value = .Value

    End With
    now all works fine.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,486

    Re: Help on Autofilter operator

    @mcguirpa: for future reference, please provide a sample workbook. It takes a lot of the guesswork out of diagnosong the problem and providng a solution.

    And ... Please don't quote whole posts -- it's just clutter.*If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ 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. [SOLVED] Autofilter not getting entire / Autofilter not applying to all columns
    By KarelMusa in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2019, 04:45 PM
  2. Replies: 1
    Last Post: 08-23-2013, 05:45 PM
  3. Replies: 4
    Last Post: 03-16-2013, 08:33 AM
  4. Replies: 2
    Last Post: 07-06-2012, 11:42 AM
  5. AutoFilter method of Range class failed - Yet autofilter works.
    By Carlsbergen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-25-2009, 05:43 PM
  6. Capturing the Excel AutoFilter Sorting Event-sort and autofilter options
    By Kognyto in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-11-2008, 05:36 PM
  7. [SOLVED] IN operator
    By Suzanne in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-10-2006, 01:30 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