Results 1 to 13 of 13

Finding date groups

Threaded View

  1. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Finding date groups

    Hello OAM,

    This macro will split the dates as described. The attached workbook has a new sheet with a button to run the macro. Here is the macro code.
    Sub SplitDates()
    
      ' http://www.excelforum.com/excel-programming-vba-macros/892036-finding-date-groups.html
      
        Dim i As Long
        Dim nbrDates As Variant
        Dim r As Long
        Dim rngDates As Range
        Dim rngOutput As Range
        Dim StartDate As Date
        Dim Wks As Worksheet
            
            
            Set rngOutput = Worksheets("Sheet4").Range("A1")
            rngOutput.CurrentRegion.ClearContents
            
            Set Wks = Worksheets("Sheet1")
            
            Set rngDates = Wks.Range("B1")
            Set rngDates = Wks.Range(rngDates, Wks.Cells(Rows.Count, "B").End(xlUp))
            
            For Each Cell In rngDates
                
                If Not IsEmpty(Cell) Then
                    Set rngDates = Wks.Range(Cell, Wks.Cells(Cell.Row, Columns.Count).End(xlToLeft))
                    nbrDates = rngDates.Value
                
                    If TypeName(nbrDates) = "Variant()" Then
                        StartDate = CDate(nbrDates(1, 1))
                
                        For i = 1 To UBound(nbrDates, 2) - 1
                            If nbrDates(1, i + 1) - nbrDates(1, i) <> 1 Then
                                rngOutput.Offset(r, 0).Value = StartDate & " to " & CDate(nbrDates(1, i))
                                r = r + 1
                                StartDate = CDate(nbrDates(1, i + 1))
                                i = i + 1
                            End If
                        Next i
                
                        rngOutput.Offset(r, 0).Value = StartDate & " to " & CDate(nbrDates(1, i))
                        r = r + 2
                    End If
                    
                End If
               
            Next Cell
               
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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