VBA DateAdd未按预期工作

时间:2016-10-05 16:10:25

标签: excel-vba vba excel

我在Excel中使用DateTime.DateAdd VBA函数来创建一列日期。使用下面的代码,当时间进展到午夜时,这一天不会像预期的那样改变。

Sub test()
    Dim i As Integer
    Dim currentDate As Date

    With ThisWorkbook.Sheets(1)
        .Cells.Clear
        .Cells(1, 1) = "DateTime"
        .Cells(1, 2) = "Day"

        currentDate = CDate("10/3/2016 11:59:30 PM")

        For i = 1 To 10
            .Cells(i + 1, 1) = currentDate
            .Cells(i + 1, 2) = DateTime.Day(currentDate)
            currentDate = DateTime.DateAdd("s", 5, currentDate)
        Next i

        .Columns(1).NumberFormat = "m/d/yyyy h:mm:ss AM/PM"
    End With
End Sub

输出:

  • DateTime |天
  • 10/3/2016 11:59:30 PM | 3
  • 10/3/2016 11:59:35 PM | 3
  • 10/3/2016 11:59:40 PM | 3
  • 10/3/2016 11:59:45 PM | 3
  • 10/3/2016 11:59:50 PM | 3
  • 10/3/2016 11:59:55 PM | 3
  • 10 / 3 / 2016 12:00:00 AM | 4 (日值不匹配!)
  • 10/4/2016 12:00:05 AM | 4
  • 10/4/2016 12:00:10 AM | 4
  • 10/4/2016 12:00:15 AM | 4

将数字格式更改为军事时间会产生类似的结果。如果我使用输出日期作为图表的x值,则图表将使用“不正确”的日期值。但是,如果我将代码中的开始时间更改为10/3/2016 11:59:45 PM,则日期会按预期更改。我做错了什么或者是否有其他更稳定的方法来为VBA中的日期添加时间?

编辑:

mrbungle和Comintern提供了出色的解决方法。他们都达到了相同的最终结果,但我更喜欢mrbungle的方法,因为它避免改变价值,虽然可以忽略不计。

总结替代方案:

Sub test()
    Dim i As Integer
    Dim currentDate As Date

    With ThisWorkbook.Sheets(1)
        .Cells.Clear
        .Cells(1, 1) = "DateTime"
        .Cells(1, 2) = "Day"

        currentDate = CDate("10/3/2016 11:59:30 PM")

        For i = 1 To 10
            .Cells(i + 1, 1) = CStr(currentDate) ' mrbungle's method
            .Cells(i + 1, 1) = currentDate + 0.000000001 ' Comintern's method
            .Cells(i + 1, 2) = DateTime.Day(currentDate)
            currentDate = DateTime.DateAdd("s", 5, currentDate)
        Next i

        .Columns(1).NumberFormat = "m/d/yyyy h:mm:ss AM/PM"
    End With
End Sub

2 个答案:

答案 0 :(得分:2)

这似乎是一个Excel格式错误。实际上,VBA值是正确的:

Sub Example()
    Dim i As Integer
    Dim currentDate As Date
    currentDate = CDate("10/3/2016 11:59:30 PM")
    For i = 1 To 10
        Debug.Print currentDate, Day(currentDate)
        currentDate = DateTime.DateAdd("s", 5, currentDate)
    Next i
End Sub

输出:

10/3/2016 11:59:30 PM        3 
10/3/2016 11:59:35 PM        3 
10/3/2016 11:59:40 PM        3 
10/3/2016 11:59:45 PM        3 
10/3/2016 11:59:50 PM        3 
10/3/2016 11:59:55 PM        3 
10/4/2016      4 
10/4/2016 12:00:05 AM        4 
10/4/2016 12:00:10 AM        4 
10/4/2016 12:00:15 AM        4

道德似乎是,如果你在Excel中进行日期算术并且必须正确表示午夜,你应该添加一个小的delta作为变通方法:

 currentDate = CDate("10/3/2016 11:59:30 PM") + 0.00000000001

这会“欺骗”Excel使用正确的日期为12:00 AM。

答案 1 :(得分:1)

不确定这是否有资格作为答案,但确实可以解决问题。似乎粘贴日期String围绕这个问题。并且搜索它似乎是Excel添加日期和移动日期的问题。

Sub test()
Dim i As Integer
Dim currentDate As Date
Dim currentDate2 As String

With ThisWorkbook.Sheets(1)
    .Cells.Clear
    .Cells(1, 1) = "DateTime"
    .Cells(1, 2) = "Day"

    currentDate = CDate("10/3/2016 11:59:30 PM")
    currentDate2 = currentDate
    For i = 1 To 10
        .Cells(i + 1, 1) = currentDate2
        .Cells(i + 1, 2) = Day(currentDate)
        '.Cells(i + 1, 3) = test
        currentDate = DateTime.DateAdd("s", 5, currentDate)
        currentDate2 = currentDate
    Next i

    .Columns(1).NumberFormat = "m/d/yyyy h:mm:ss AM/PM"
End With
End Sub