添加缺less的dateVBA

我必须插入缺失的date到一行而不删除重复的date(对于计费程序)。 示例数据:

DATE 01/02/2016 02/02/2016 03/02/2016 03/02/2016 03/02/2016 06/02/2016 07/02/2016 08/02/2016 

我的代码是无限循环和删除重复的date。 为什么会这样呢?

 Sub InsertMissingDates() Dim i As Long Dim RowCount As Long i = 4 Do If Cells(i, 1) + 1 <> Cells(i + 1, 1) Then Rows(i + 1).Insert Cells(i + 1, 1) = Cells(i, 1) + 1 End If i = i + 1 Loop Until Cells(i + 1, 1) = "31.10.2016" End Sub 

这里是修改的代码,以解决您的问题

 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