VBA:DateDiff无效

Lee*_*Sin 2 excel vba excel-vba

我有一个excel文件,可以在添加条目时自动为其添加时间戳.我想要一个脚本,找到第一个和最后一个条目之间的时间差.这是我到目前为止所提出的.

Sub Duration()

Dim lRow As Long Dim lValue Dim fValue Dim Duration As Long, n As Integer

'Find the last non-blank cell in column A(1)
lRow = Cells(Rows.Count, 1).End(xlUp).Row
lValue = Cells(lRow, 5).Value
MsgBox (lValue)
fValue = Cells(5, 5).Value
MsgBox (fValue)
Duration = DateDiff("n", "fValue", "lValue")
MsgBox (Duration)

Cells(3, 5) = Duration 

End Sub
Run Code Online (Sandbox Code Playgroud)

Vit*_*ata 5

DateDiff 需要3个参数:

DATEDIFF ( datepart , startdate , enddate )
Run Code Online (Sandbox Code Playgroud)

所有参数也可以作为变量给出.在您的情况下,请尝试:

Duration = DateDiff("n", fValue, lValue)
Run Code Online (Sandbox Code Playgroud)

如果你想用更少的线条制作它,你甚至可以选择:

Duration = DateDiff("n", Cells(5, 5).Value, Cells(lRow, 5).Value)
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅https://msdn.microsoft.com/en-us/library/ms189794.aspx.