VBA - 减去时间

时间:2015-03-30 13:07:09

标签: vba excel-vba excel

我有一张excell表,我需要减去两个包含时间值的单元格: 18:35 - 18:02 。但是,当我减去这两个值时,我得到以下结果:12:33:00 AM。我需要得到的只是00:33:00。换句话说,这两次之间的分钟差异。 我的代码如下:

Sub calculo_setup()

Dim x As Date
Dim col As Integer
Dim lin As Integer
Dim start_time As Date
Dim end_time As Date
Dim prouzido As Integer
Dim programado As Integer


lin = 3
col = 5

Sheets("Dados").Select
Cells(7, col - 1).Select

start_time = ActiveCell

MsgBox start_time, , "A hora de início é"

Call organiza_autoclv

Sheets("Dados").Select
Cells(3, col).Select
x = 0

Do While ActiveCell = Cells(3, col - 1)
    If Cells(4, col) = Cells(4, col - 1) Then
    x = x + (Cells(7, col) - Cells(34, col - 1))
    col = col + 1
    Else
    col = col + 1

    End If
    Cells(3, col).Select
Loop


MsgBox x, , "O valor de setup_g5 é"
End Sub

2 个答案:

答案 0 :(得分:1)

使用Format()

x = x + format(Cells(7,col) - Cells(34,col - 1),“hh:mm:ss”)

基于Sobigen在评论中的建议:

MsgBox format(x, "mm/dd/yyyy hh:mm:ss"), , "O valor de setup_g5 é"

我在初次回复中错过了这一行

答案 1 :(得分:0)

Date数据类型的单位是1天。因此,如果您想要经过的分钟数,可以使用:

MsgBox x * 24 * 60