Add missing dates VBA

前端 未结 1 932
小鲜肉
小鲜肉 2021-01-23 06:44

I have to insert missing dates to a row without deleting the duplicated dates (for a billing program). Example data:

DATE
01/02/2016    
02/02/2016    
03/02/20         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 07:25

    Here is the code modified with comments to address your issues

    Sub InsertMissingDates()
    
    Dim i As Long
    Dim RowCount As Long
    
    i = 4
    
    Do
        'Use less then instead of <> so it doesn't flag duplicate cells
        If Cells(i, 1) + 1 < Cells(i + 1, 1) Then
            Rows(i + 1).Insert
            Cells(i + 1, 1) = Cells(i, 1) + 1
        End If
        'Second check to add value if the next row is blank
        If (Cells(i + 1, 1) = "") Then
            Cells(i + 1, 1) = Cells(i, 1) + 1
        End If
    
        i = i + 1
    'Changed the loop function from cells(i+1,1) to cells(i,1) since you already
    'incremented i
    'Also made the date check slightly more robust with dateserial
    Loop Until Cells(i, 1).Value >= DateSerial(2016, 1, 30)
    
    End Sub
    

    0 讨论(0)
提交回复
热议问题