我下面的代码正在做某种持续时间计算,但是在运行时它抛出了一个异常type mismatch
,尽管这两个参数都包含date values
在其中。那么有什么建议是错误在哪里吗?
代码:
Function TimeSpan(dt1,dt2)
Dim dtTemp
objExcel1.Application.ScreenUpdating = False
If (IsDate(dt1) And IsDate(dt2)) = False Then
TimeSpan = "00:00:00"
Exit Function
End If
If dt2 < dt1 Then
dtTemp = dt2
dt2 = dt1
dt1 = dt2
End If
'-- since you only had days, I have put up to days here.
'-- if you require months, years you may use yy:mm:dd:hh:mm:ss
'-- which is pretty self-explainatory ;)
Msgbox("DT2:" & dt2 & "DT1:" & dt1)
TimeSpan = objExcel1.Application.WorksheetFunction.Text((dt2 - dt1), "[h]:mm:ss")'"dd:hh:mm:ss"
objExcel1.Application.ScreenUpdating = True
End Function
@Tomalak我已经按照您的要求更新了代码,现在出现错误。
代码
Function TimeSpan(dt1, dt2)
If Not (IsDate(dt1) And IsDate(dt2)) Then
TimeSpan = "00:00:00"
ElseIf dt2 < dt1 Then
TimeSpan = TimeSpan(dt2, dt1)
Else
MsgBox((CDate(dt1) - CDate(dt2)))
TimeSpan = objExcel1.Application.WorksheetFunction.Text( (CDate(dt1) - CDate(dt2)), "[h]:mm:ss" )
End If
End Function
Msgbox Displaying: