Jas*_*son 4 excel vba excel-vba
我正在尝试计算在Excel中完成分析所需的时间.我正在使用DateDiff来计算它所需的秒数,然后尝试以小时,分钟,秒为格式.
我的代码在下面导致溢出错误:
dTime = dTime / (60 * 60 * 24)
Run Code Online (Sandbox Code Playgroud)
你知道我做错了什么吗?
Dim dTime As Long
Dim startTime, endTime As Date
startTime = Now() ' at the start of the analysis
endTime = Now() ' at the end of the analysis
dTime = DateDiff("s", startTime, endTime)
dTime = dTime / (60 * 60 * 24) 'convert seconds into days
StatusBox.Value = "Analysis completed in " & Format(dTime, "hh:mm:ss") & "."
Run Code Online (Sandbox Code Playgroud)
小智 6
看来你过度补偿了.无需转换.简单地从结束时间中减去开始时间(可选地存储为@MatthewD提到的双精度)并将单元格格式化为hh:mm:ss(首选)或表示时间的字符串Format(dTime, "hh:mm:ss")
.
Dim dTime As Double
Dim startTime As Date, endTime As Date
startTime = Now() ' at the start of the analysis
'analysis here
endTime = Now() ' at the end of the analysis
dTime = endTime - startTime
StatusBox.Value = "Analysis completed in " & Format(dTime, "hh:mm:ss") & "."
Run Code Online (Sandbox Code Playgroud)
如果您希望它是Date类型var,则您的startTime声明不正确.您使用的方法将其创建为变体.