VBA:需要计算不同日期之间的时差

时间:2022-09-27 08:46:21

I need help with calculating time difference between times from two different dates.

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

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

The answer should be: 25:30:00 Following is my code:

答案应该是: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)

But this code gives me 1:30:00

但是这段代码给了我1:30:00

1 个解决方案

#1


3  

Just subtract:

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

VBA:需要计算不同日期之间的时差

#1


3  

Just subtract:

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

VBA:需要计算不同日期之间的时差