+ Reply to Thread
Results 1 to 23 of 23

identify earliest & latest date groups in table

Hybrid View

Brontosaurus identify earliest & latest... 07-17-2011, 07:15 AM
MickG Re: identify earliest &... 07-17-2011, 09:11 AM
Brontosaurus Re: identify earliest &... 07-17-2011, 10:34 AM
MickG Re: identify earliest &... 07-17-2011, 10:53 AM
Brontosaurus Re: identify earliest &... 07-17-2011, 01:22 PM
  1. #1
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    identify earliest & latest date groups in table

    Hi,

    I need to create some code which will select the earliest and latest dates from a table (attached) for each unique combination of two columns (work order - col L & task id - col M) and put them into a specified existing worksheet.

    Can anyone help please?

    Thanks

    Louise
    Attached Files Attached Files
    Last edited by Brontosaurus; 07-19-2011 at 04:27 PM.

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: identify earliest & latest date groups in table

    Try this:-
    Results start sheet (2) "A1".
    Sub MG17Jul25
    Dim Rng     As Range
    Dim Dn      As Range
    Dim Q       As Variant
    Dim Twn     As String
    
    Set Rng = Range(Range("L2"), Range("L" & Rows.Count).End(xlUp))
        With CreateObject("scripting.dictionary")
           .CompareMode = vbTextCompare
    For Each Dn In Rng
        Twn = Dn & Dn.Offset(, 1)
            If Not .Exists(Twn) Then
                .Add Twn, Array(Dn, Dn(, 2), Dn(, 3), Dn(, 4))
            Else
                Q = .Item(Twn)
                If Dn(, 3) < (Q(2)) Then Q(2) = Dn(, 3)
                If Dn(, 4) > (Q(3)) Then Q(3) = Dn(, 4)
                .Item(Twn) = Q
            End If
    
    Next
     Sheets("sheet2").Range("A1").Resize(.Count, 4) = Application.Index(.items, Evaluate("row(" & 1 & ":" & .Count & " )"), Array(1, 2, 3, 4))
    End With
    End Sub
    Regards Mick

  3. #3
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    Re: identify earliest & latest date groups in table

    Hi Mick,

    Thanks for offering a solution to this.

    The code almost works but for some reason the dates are in different formats and or transposed and don't seem to want to re-format.

    Eg 01/08/11 is being returned as 08/01/11 and some dates are shown as dd/mm/yy and others dd/mm/yyyy.

    Please would you take another look at it?

    Thanks,

    Louise

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: identify earliest & latest date groups in table

    Try this:-
    If this does not work try formatting the columns where the dates will be returned.
    Sub MG17Jul11
    Dim Rng     As Range
    Dim Dn      As Range
    Dim Q       As Variant
    Dim Twn     As String
    Dim Dt1 As Date
    Dim Dt2 As Date
    Set Rng = Range(Range("L2"), Range("L" & Rows.Count).End(xlUp))
        With CreateObject("scripting.dictionary")
           .CompareMode = vbTextCompare
    For Each Dn In Rng
        Twn = Dn & Dn.Offset(, 1)
            Dt1 = Format(Dn(, 3), "dd/mm/yyyy")
            Dt2 = Format(Dn(, 4), "dd/mm/yyyy")
            If Not .Exists(Twn) Then
                .Add Twn, Array(Dn, Dn(, 2), Dt1, Dt2)
            Else
                Q = .Item(Twn)
                If Dt1 < DateValue(Q(2)) Then Q(2) = Dt1
                If Dt1 > DateValue(Q(3)) Then Q(3) = Dt2
                .Item(Twn) = Q
            End If
    
    Next
     Sheets("sheet2").Range("A1").Resize(.Count, 4) = Application.Index(.items, Evaluate("row(" & 1 & ":" & .Count & " )"), Array(1, 2, 3, 4))
    End With
    End Sub
    Regards Mick

  5. #5
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Suffolk, England
    MS-Off Ver
    Office 365
    Posts
    271

    Re: identify earliest & latest date groups in table

    Hi again Mick,

    This time I get a run time error 13 - type mismatch error.

    I've tried formatting the columns but makes no difference.

    It must be almost there, please would you suggest a fix?

    Thanks

    Louise

  6. #6
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: identify earliest & latest date groups in table

    Have you got any cells in those 4 columns that are blank or cells that should be a date, and are not.

+ 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