我试图做以下事情:
startDate = Date
endDate = DateAdd("yyyy", -1, startDate)
currentDate = startDate
Do While DateDiff("d", endDate, currentDate) <> 0
With ActiveSheet.Range("A1")
.Offset(i, 0).Value = currentDate
End With
currentDate = DateAdd("d", -1, currentDate)
i = i + 1
Loop
从startDate循环到endDate并输出其间的每个日期。在第一次迭代之后,currentDate等于最小日期(1899)。 我知道还有其他方法可以做到这一点,我现在有办法,但为什么会失败?
答案 0 :(得分:4)
你的代码对我来说很好
Yoo实际上可以通过
来避免循环 <强> code
强>
Sub Test2()
Dim x
Dim dtStart As Date
Dim lngStart As Long
'test for leap year
lngStart = 365
If Year(Now()) Mod 4 = 0 Then
If Year(Now()) Mod 25 = 0 Then lngStart = lngStart + 1
End If
dtStart = Date
x = Application.Evaluate("NOW()-row(1:" & lngStart & ")+1")
With [a1].Resize(UBound(x, 1), 1)
.Value = x
.NumberFormat = "d/mm/yyyy;@"
End With
End Sub