我试图编写一个脚本来搜索日期列表,并确定日期间隔有多长。我是新来的VBA,这可能是完全错误的,但是引用几个网站后,这里是我想出了:确定日期差距给定日期列表的长度
Sub IdentifyGaps()
Dim startdate As Date 'first date in column
Dim enddate As Date 'last date in column
Dim ust As Date 'first date of unemployment
Dim i As Long
ust = ActiveCell.Offset(1, 0).Value
With Sheet6
startdate = [A1]
enddate = .Cells(.Rows.Count, "A").End(xlUp).Value
For i = startdate To enddate
If ust <> DateAdd("d", 1, i) Then
Sheet6.[C1].Value = DateDiff("d", i, ust)
End If
Next i
End With
End Sub
我没有收到一个错误,但宏无法正常工作。现在,它应该返回15时返回-43074.任何帮助将非常感谢!
下面是数据的屏幕截图,其中应该显示唯一的日期差距。
尝试使用'结束日期= .Cells(.Rows.Count, “A”)结束(xlUp).Value'而不是行 – xthestreams
你有一个可变 - 'B' - 未在规定你提供了什么,这会引发错误(至少在我这边)。你能否提供你的工作表数据的视觉,以便我们可以更好地了解它的结构? – TotsieMae
@xthestreams谢谢你 - 这确实使宏工作;但是,给出的值是“-43074”,这意味着它尚未正常工作。 – Kim