Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Excel中将秒正确转换为hh:mm:ss_Excel_Vba - Fatal编程技术网

如何在Excel中将秒正确转换为hh:mm:ss

如何在Excel中将秒正确转换为hh:mm:ss,excel,vba,Excel,Vba,我试图计算在Excel中完成分析所需的时间。我使用DateDiff来计算它所需的秒数,然后尝试以小时、分钟、秒为单位格式化它 下面的代码导致第行出现溢出错误: dTime = dTime / (60 * 60 * 24) 你知道我做错了什么吗 Dim dTime As Long Dim startTime, endTime As Date startTime = Now() ' at the start of the analysis endTime = Now() ' at the end

我试图计算在Excel中完成分析所需的时间。我使用DateDiff来计算它所需的秒数,然后尝试以小时、分钟、秒为单位格式化它

下面的代码导致第行出现溢出错误:

dTime = dTime / (60 * 60 * 24)
你知道我做错了什么吗

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") & "."

看来你补偿过度了。不需要转换。只需从结束时间中减去开始时间(可以选择存储为@MatthewD提到的双精度),并将单元格格式化为hh:mm:ss(首选)或使用
格式(dTime,“hh:mm:ss”)
表示时间的字符串

如果希望startTime声明为日期类型变量,则该声明不正确。您使用的方法将其创建为变量。

尝试此操作

Sub seconds()
Dim dTime As Variant
Dim startTime, endTime As Date

startTime = #8/7/2015 10:43:32 PM# ' at the start of the analysis
endTime = Now() ' at the end of the analysis

dTime = DateDiff("s", startTime, endTime)
dTime = dTime / 86400
MsgBox "Analysis completed in " & Format(dTime, "hh:mm:ss") & "."

End Sub

您可能需要dTime,因为Double
startTime
在这里被声明为变量。您需要明确说明:
Dim startTime as Date
Sub seconds()
Dim dTime As Variant
Dim startTime, endTime As Date

startTime = #8/7/2015 10:43:32 PM# ' at the start of the analysis
endTime = Now() ' at the end of the analysis

dTime = DateDiff("s", startTime, endTime)
dTime = dTime / 86400
MsgBox "Analysis completed in " & Format(dTime, "hh:mm:ss") & "."

End Sub