【发布时间】:2018-09-09 17:06:45
【问题描述】:
我有一个包含 40,000 行数据的数据集。我的代码设置为检查第 n+1 行中的日期是否比第 n 行中的日期晚 1 天。如果第 n 行和第 n+1 行中的日期没有按照正常的时间顺序排列,则它会添加一行包含该日期的空白数据。
我的问题是,因为我正在添加行,所以我不知道我的 for 循环应该有什么结束范围。我还尝试设置一个非常大的范围,例如“对于 n = 2 到 50000”。但这给了我一个溢出错误。
这是我的代码:
Sub MissingDates()
Dim n As Integer
Worksheets("sheet1").Activate
For n = 2 To 40000
If Cells(n, 2).Value <> Cells(n + 1, 2).Value - 1 Then
Cells(n + 1, 2).EntireRow.Insert Shift:=xlShiftDown
Cells(n + 1, 2) = Cells(n, 2) + 1
End If
Next
End Sub
提前感谢您的帮助。
【问题讨论】:
-
Dim n As long和For n = cells(rows.coun, "B").end(xlup).row - 1 To 2 step-1