Results 1 to 9 of 9

Thread: Add Missing Dates

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    there might be a better solution. In the meantime try this.

    Code:
    Sub kTest()
        
        Dim d   As Date, ed As Date
        Dim r   As Range, c As Range
        Dim i   As Long
        
        Set r = Range("d" & Rows.Count).End(xlUp)
        d = CDate(r.Value2)
        ed = DateSerial(Year(d), Month(d) + 1, 1) - 1
        
        For i = r.Row To 1 Step -1
            Set c = Cells(i, r.Column)
            d = CDate(c.Value2)
            If ed - d > 1 Then
                c.Offset(1).Resize(Day(ed) - Day(d) - 1).EntireRow.Insert
                c.AutoFill c.Resize(Day(ed) - Day(d) + 1)
            End If
            ed = CDate(d)
        Next
        If Day(ed) > 1 Then
            c.Resize(Day(ed) - 1).EntireRow.Insert
            Cells(1, r.Column) = DateSerial(Year(ed), Month(ed), 1)
            Cells(1, r.Column).AutoFill Cells(1, r.Column).Resize(Day(ed))
        End If
    
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #2
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    15
    Hi

    Excellent solution, with this cycle.

  3. #3
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    15
    Hi,

    A modification to the code to operate from a different range

    Code:
    Sub kTest21()
        
        Dim d   As Date, ed As Date
        Dim r   As Range, c As Range
        Dim i   As Long
        Dim list   As Long:    list = 21
        Set r = Range("d" & Rows.Count).End(xlUp)
    
        d = CDate(r.Value2)
    
        ed = DateSerial(Year(d), Month(d) + 1, 1) - 1
        
        For i = r.Row To 1 Step -1
            Set c = Cells(i, r.Column)
            
            If Val(c.Value2) = 0 Then Exit For
            
            d = CDate(c.Value2)
            If ed - d > 1 Then
                c.Offset(1).Resize(Day(ed) - Day(d) - 1).EntireRow.Insert
                c.AutoFill c.Resize(Day(ed) - Day(d) + 1)
            End If
            ed = CDate(d)
        Next
        If Day(ed) > 1 Then
            c.Offset(1).Resize(Day(ed - 1)).EntireRow.Insert
            Cells(list, r.Column) = DateSerial(Year(ed), Month(ed), 1)
            Cells(list, r.Column).AutoFill Cells(list, r.Column).Resize(Day(ed))
        End If
    
    End Sub
    changes suggestion

Similar Threads

  1. Missing Numbers Range VBA
    By Rhett in forum Excel Help
    Replies: 2
    Last Post: 10-27-2013, 10:43 PM
  2. Insert Missing Dates By Comparing Two Lists Of Dates
    By mahmoud-lee in forum Excel Help
    Replies: 24
    Last Post: 10-16-2013, 04:48 PM
  3. Replies: 4
    Last Post: 04-05-2013, 12:08 PM
  4. How to Extracting dates and days between 2 dates.
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 9
    Last Post: 08-11-2012, 09:11 PM
  5. Missing MSComctllib
    By Rasm in forum Excel Help
    Replies: 3
    Last Post: 04-05-2011, 09:45 PM

Posting Permissions

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