Consulting

Results 1 to 4 of 4

Thread: Autofilter latest event date

  1. #1

    Autofilter latest event date

    Hi, there
    Would you please to write the macro for filtering latest record with the header Event Date. Kindly note that the event date is text format instead of date format.

    Remark: Enclosed the attachment with 2 tabs. I would like the result as per the file.
    Attached Files Attached Files

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Give this a try. I can't test it properly because of the crazy American date convention!
    Sub Test()
        LR = Cells(Rows.Count, 7).End(xlUp).Row
        Columns("$G:$G").AutoFilter
        Range("$G$4:$G" & LR).AutoFilter Field:=1, Criteria1:="=" & LastDate(7) & "*"
    End Sub
    
    
    Function LastDate(Col) As String
        Dim r As Range, dt As Date, ld As Date
        Set r = Range(Cells(5, Col), Cells(Rows.Count, Col).End(xlUp))
        ld = 0
        For Each cel In r
            dt = DateValue(Left(cel, 10))
            If dt > ld Then ld = dt
        Next
        LastDate = CStr(ld)
    End Function
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Using DateValue is the most straightforward solution, but if that doesn't work for you then we can be explicit about the dates by changing:
    dt = DateValue(Left(cel, 10))
    to:
    dt = DateSerial(Mid(cel, 7, 4), Left(cel, 2), Mid(cel, 4, 2))


    and:
    LastDate = CStr(ld)
    to:
    LastDate = Format(ld, "mm/dd/yyyy")
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Hi, mdmackillop....Thank you for quick response!! However, it's not workable.

    Hi, p45cal.............Thank you for reviewing!!! After changing code, it is workable now.

    Sub Test()
        LR = Cells(Rows.Count, 7).End(xlUp).Row
        Columns("$G:$G").AutoFilter
        Range("$G$4:$G" & LR).AutoFilter Field:=1, Criteria1:="=" & LastDate(7) & "*"
    End Sub
    
    
    Function LastDate(Col) As String
        Dim r As Range, dt As Date, ld As Date
        Set r = Range(Cells(5, Col), Cells(Rows.Count, Col).End(xlUp))
        ld = 0
        For Each cel In r
            dt = DateSerial(Mid(cel, 7, 4), Left(cel, 2), Mid(cel, 4, 2))
            If dt > ld Then ld = dt
        Next
        LastDate = Format(ld, "mm/dd/yyyy")
    End Function
    Last edited by JOEYSCLEE; 07-31-2017 at 05:59 AM.

Tags for this Thread

Posting Permissions

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