VBA:DateDiff不起作用

我有一个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 

DateDiff需要3个参数:

 DATEDIFF ( datepart , startdate , enddate ) 

所有参数也可以作为variables给出。 在你的情况下,尝试:

 Duration = DateDiff("n", fValue, lValue) 

如果你喜欢用更less的线条来制作,你甚至可以去:

 Duration = DateDiff("n", Cells(5, 5).Value, Cells(lRow, 5).Value) 

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