2016-06-25 346 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