+ Reply to Thread
Results 1 to 23 of 23

identify earliest & latest date groups in table

Hybrid View

  1. #1
    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

  2. #2
    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

  3. #3
    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

  4. #4
    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

  5. #5
    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.

  6. #6
    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

    The destination worksheet and cells are blank.

    Thanks

    Louise

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

    Re: identify earliest & latest date groups in table

    Sorry, what I meant was, are any of the Data cells That should have Text Numbers or Dates Blank or any of the Data cells that should contain Dates, Blank.
    Example :- Your data is in Columns "L to O", if say there are 100 rows in those columns with data if any cell within this range is blank or has not got a date, you could get that error.
    If there are blanks and that is how your data would normally be presented to you, then I can perhaps alter the code to take account of that, but I was trying to understand where the error came from.
    Mick

+ 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