gpt4 book ai didi

VBA:DateDiff 不工作

转载 作者:行者123 更新时间:2023-12-02 18:58:41 30 4
gpt4 key购买 nike

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

所有参数也可以作为变量给出。根据您的情况,请尝试:

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

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

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

详情参见 https://msdn.microsoft.com/en-us/library/ms189794.aspx .

关于VBA:DateDiff 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42353199/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com