2016-06-25 347 views
0

我需要帮助计算两个不同日期之间的时间差。VBA:需要计算不同日期之间的时间差

Cell(1,1) contains: 19/06/2016 01:00:00 
Cell(1,2) contains: 20/06/2016 02:30:00 

答案应该是:25:30:00 以下是我的代码:

Dim a As Date, b As Date  
a = Cells(1, 1).Value 
b = Cells(1, 2).Value 
Cells(1, 3).Value = TimeValue(b) - TimeValue(a) 

但这个代码给了我1:30:00

+0

[如何计算VBA中的时差?](http://stackoverflow.com/questions/28475288/how-to-calculate-time-difference-in-vba) – BitAccesser

+1

'TIMEVALUE'函数返回十进制数由文本字符串表示的时间。十进制数是从0(零)到0.99988426的值,表示从0:00:00(12:00:00 AM)到23:59:59(晚上11:59:59)的时间。因此,您使用'TimeValue(b) - TimeValue(a)'是不正确的。我认为@加里的学生解决方案是正确的做法。 – skkakkar

回答

3

就减:

Sub luxation() 
    Dim a As Date, b As Date 
    a = Cells(1, 1).Value 
    b = Cells(1, 2).Value 
    Cells(1, 3).Value = b - a 
    Cells(1, 3).NumberFormat = "[hh]:mm:ss" 
End Sub 

enter image description here