Results 1 to 6 of 6

Thread: [RESOLVED] Autofilter Criteria Array Parameters

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Resolved [RESOLVED] Autofilter Criteria Array Parameters

    In this autofilter statement

    .Range("$A$1:$D$233").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, "9/1/2021")

    what does the bolded 2 represent?

    And how can I change the Array() portion to use a string variable which contains a date?

  2. #2

  3. #3
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,392

    Re: Autofilter Criteria Array Parameters


  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Autofilter Criteria Array Parameters

    but strDate need to be mm/dd/yyyy
    maybe
    Code:
    strdate = "9/1/21"
    .Range("$A$1:$D$233").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, Format(CDate(strdate), "mm/dd/yyyy")
    should fix issues, but i just hate ambiguous dates
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,417

    Re: Autofilter Criteria Array Parameters

    The question is not which output-format to use, but which format the input-values have

    Is "9/1/2021" the 1st of September 2021 or the 9th of January 2021?
    If the Input-Format is known, and definitely doesn't change, i'd rather parse through the Date-String, assign separate values for day, month and year, and then use DateSerial to recombine it into a valid, local Date
    In case of String-Dates i'd go with the Split-Function
    Code:
    Dim arrDate() As String
    Dim datDay As Long
    Dim datMonth As Long
    Dim datYear As Long
    Dim MyDate As Date
    
    arrDate=Split("9/1/2021","/")
    
    'I'm assuming the Sample date is 1st of Sep.
    datDay=CLng(arrDate(1))
    datMonth=CLng(arrDate(0))
    datYear=CLng(arrDate(2))
    MyDate=DateSerial(datYear, datMonth, datDay)  '--> the local variables are just to demonstrate. You could use the Array-Memebers directly in the Function
    'Use whatever Format of MyDate you need in subsequent Functions
    Last edited by Zvoni; Oct 7th, 2021 at 05:21 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

Posting Permissions

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



Click Here to Expand Forum to Full Width