1

因此,我花了 4 个小时的时间来研究这段代码,从头开始重写。我唯一得到的就是将代码从大约 15 行减少到现在的 9 行。

无论如何,代码的问题在于它没有正确识别带有 intArrayIndex-1 的 arrDateTime 日期。而不是正常日期说“2013 年 6 月 7 日”,我可以在 k = 0 if 语句中检索它,它返回“1899 年 12 月 30 日”,当日期语句不正确时,我读到它。

此外,我尝试使用特定数字来测试它们,并且在 j=0 部分没有问题,但是,由于某种原因 j=1 语句不起作用。我还尝试通过在 DateAdd 变量中使用 1-j 而不是 1 来简化代码,但是它不想添加任何天数。

'grabs date and time
If (k = 0) Then
    intDay = Cells(intRowNum + 2, 2).Value
    arrDateTime(intArrayIndex) = DateValue(strMonth & " " & intDay & ", " & intYear) + (Cells(intRowNum + intMaxRows + 3, 1).Value)
ElseIf j = 0 Then
    arrDateTime(intArrayIndex) = DateAdd("d", 1, arrDateTime(intArrayIndex - 1))
ElseIf j = 1 Then
    arrDateTime(intArrayIndex) = arrDateTime(intArrayIndex - 1)
End If

我在这里变得绝望,任何帮助找出错误使用日期变量的原因将不胜感激。

更新 1

根据要求,我已经包含了与问题相关的所有代码,我没有包含所有内容,因为它有 400 多行。

Dim intRowNum As Integer
Dim intMaxRows As Integer
Dim intArrayIndex As Integer
Dim intYear As Integer
Dim intDay As Integer
Dim strMonth As String

Dim arrTitle(0 To 9) As String
Dim arrDescription(0 To 9) As String
Dim arrProf(0 To 9) As String
Dim arrDateTime(9) As Date

For j = 0 To 1
     For k = 0 To 4
        intArrayIndex = k * 2 + j

        'grabs date and time
        If (k = 0) Then
            intDay = Cells(intRowNum + 2, 2).Value
            arrDateTime(intArrayIndex) = DateValue(strMonth & " " & intDay & ", " & intYear) + (Cells(intRowNum + intMaxRows + 3, 1).Value) 
        ElseIf j = 0 Then
            arrDateTime(intArrayIndex) = DateAdd("d", 1, arrDateTime(intArrayIndex - 1))
        ElseIf j = 1 Then
            arrDateTime(intArrayIndex) = arrDateTime(intArrayIndex - 1)
        End If

    Next
Next

For j = 0 To 9

    ActiveSheet.Cells(3 + j, 1).Value = j + 1
    ActiveSheet.Cells(3 + j, 5).Value = TimeValue(arrDateTime(j))
    ActiveSheet.Cells(3 + j, 6).Value = MonthName(month(arrDateTime(j)))
    ActiveSheet.Cells(3 + j, 7).Value = Day(arrDateTime(j))
    ActiveSheet.Cells(3 + j, 8).Value = Year(arrDateTime(j))

Next
4

1 回答 1

0

您的迭代如下:

Turn   j    k    intArrayIndex    arrDateTime(intArrayIndex)
   1   0    0                0    is created your date on first array position
   2   0    1                2    arrDateTime(2 -1) does not exist, it is empty/zero!!

因此试图用你所拥有的将 1 天的结果加到零。

于 2013-06-07T19:00:14.510 回答