如何解决两个涉及时间的文本框之和为负的问题

时间:2018-12-20 12:38:13

标签: excel vba

我有一个用户窗体,其中有两个文本框,用户在其中输入停机时间,并将其放在列表框中,现在在同一用户窗体中,我还有另一个禁用的文本框,以分钟为单位显示这两个数字的总和通常,代码运行良好,问题出在例如:用户在“ txtTStart”文本框中输入2300,在“ txtTEnd”文本中输入0010时,它会带来-1370数字。我的问题是如何为该方法获得正确的总和。

Private Sub cmdNext_Click()

Dim x As Integer
Dim totaldt As Double
Dim r As Long
Dim tdt As Double
Dim tdtt As Double

If Trim(Me.txtTStart.Value) = "" Then
    Me.txtTStart.SetFocus
    MsgBox "Please Enter Time Start"
    Exit Sub
End If
If Trim(Me.txtTEnd.Value) = "" Then
    Me.txtTEnd.SetFocus
    MsgBox "Please Enter Time End"
    Exit Sub
End If
If Trim(Me.cmbCode.Value) = "" Then
    Me.cmbCode.SetFocus
    MsgBox "Please Enter Downtime Code"
    Exit Sub
End If
If Trim(Me.txtDesc.Value) = "" Then
    Me.txtDesc.SetFocus
    MsgBox "Please Enter Downtime Description"
    Exit Sub
End If
If Trim(Me.txtDesc.Value) = "INVALID DOWNTIME CODE" Then
    Me.txtDesc.SetFocus
    MsgBox "Please Insert Valid Downtime Code"
    Exit Sub
End If

    tdt = Format((CDate(txtTEnd.Value) - CDate(txtTStart.Value)) * 1440)

tdtt = Round(tdt)

x = lstDes.ListCount

With lstDes
         .AddItem
         .List(x, 0) = txtTStart.Value
         .List(x, 1) = txtTEnd.Value
         .List(x, 2) = tdtt
         .List(x, 3) = cmbCode.Value
         .List(x, 4) = txtDesc.Value

End With

totaldt = 0
With lstDes
    For r = 0 To .ListCount - 1
        totaldt = totaldt + .List(r, 2)
    Next r
End With

txtDT.Value = totaldt


Me.txtTStart.Value = ""
Me.txtTEnd.Value = ""
'Me.txtTot.Value = ""
Me.cmbCode.Value = ""
Me.txtDesc.Value = ""

End Sub

1 个答案:

答案 0 :(得分:0)

请在该行之后将以下行:if tdt<0 then tdt=tdt+1440添加到您的代码中:

tdt = Format((CDate(txtTEnd.Value) - CDate(txtTStart.Value)) * 1440)