0

我试图做以下事情:

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)。我知道还有其他方法可以做到这一点,而且我现在有一种可行的方法,但是为什么会失败?

4

1 回答 1

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
于 2012-09-24T02:34:51.910 回答