我一直在努力解决在excel VBA中使用DateAdd
的问题。我得到Runtime Error 6 Overflow
(最常见)或Runtime Error 11 Division by Zero
。我在代码中标记了始终发生运行时错误。我试过更改变量类型但不确定问题是什么。我从Excel工作表中读取的日期采用Date
格式(mm / dd / yy)。有关为什么我在该行收到错误的任何建议?
Sub date_diff()
Dim todDate As Date
Dim dt
Dim diff As Long
Dim dates(0 To 9) As Date
Dim i As Long
todDate = ActiveWorkbook.Sheets("Investing - Overview").Range("B6").Value
' dt is the Date of last signaling
dt = ActiveWorkbook.Sheets("Investing - Overview").Range("B5").Value
diff = DateDiff("d", dt, todDate)
Dim rng As Range
Dim dtCell As Range
Dim currDt As Date
If diff < 32 Then
MsgBox "Wait " & (32 - diff) & " days"
Else
For i = 1 To 10
currDt = DateAdd("d", 20, todDate) -> RUNTIME ERROR HERE
Set rng = ActiveWorkbook.Sheets("US Stocks").Range("A5").End(xlDown)
dates(i) = currDt
MsgBox i
Next i
End If
End Sub