【问题标题】:How to fix negative in sum of two textboxes involving time如何修复两个涉及时间的文本框之和的负数
【发布时间】:2018-12-20 12:38:12
【问题描述】:

我有一个用户窗体,其中有两个文本框,用户在其中输入停机时间并将其放入列表框中,现在在同一个用户窗体中,我有另一个禁用的文本框,显示这两个文本的所有数字的总和(以分钟为单位)有时,代码运行良好,问题在于例如:用户在“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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    请将此行:if tdt<0 then tdt=tdt+1440 添加到您的代码行之后:

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

    【讨论】:

    • 谢谢!你把我从令人头疼的世界中拯救出来。
    • :):):):):):):):)
    猜你喜欢
    • 2010-09-19
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2018-08-16
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多