使用 VBA 在 excel 中计算从当前时间经过的时间
Calculating time elapsed from current time in excel using VBA
在我的程序中,用户填写一个用户表单,该表单自动生成在另一个 excel sheet 上填写的信息和时间。我想使用 VBA 来计算从表单的原始输入时间到当前时间的持续时间。我不明白为什么我的代码不起作用。当我尝试使用它时,持续时间一直为零。我不确定我需要从“Now()”中减去什么才能完成这项工作并给我一个实际值。
代码如下:
Private Sub cmdOkUpdate_Click()
Dim length As Date
length = Format(Now(), "hh:mm:ss")
strBOL = txtBOL.Value
strID = txtID.Value
details = txtDet.Value
opt = lbxOption.Value
currtime = time()
today = Format(Now(), "MM/DD/YYYY")
emp = TextBox1.Value
dur = Format(Now() - currtime, "hh:mm:ss")
If NoFill = True Then
cellFill = ""
ElseIf NoFill = False Then
With Sheet5
.Range("A1").Value = "Time"
.Range("B1").Value = "Date"
.Range("C1").Value = "Location"
.Range("D1").Value = "Category"
.Range("E1").Value = "BOL"
.Range("f1").Value = "Trailer #"
.Range("g1").Value = "Details"
.Range("H1").Value = "EE Name"
.Range("I1").Value = "Duration"
.Range("A2").EntireRow.Insert
.Range("A2").Value = currtime
.Range("B2").Value = today
.Range("C2").Value = spot
.Range("D2").Value = opt
.Range("E2").Value = strBOL
.Range("F2").Value = strID
.Range("G2").Value = details
.Range("H2").Value = emp
.Range("I2").Value = dur
.Columns("A:I").AutoFit
End With
If Not IsEmpty(opt) Then
cellFill = opt & " " & vbCrLf & "BOL (last 5 digits): " & strBOL & " " & vbCrLf & "Trailer # " & strID & " " & vbCrLf & "Details: " & details & " " & vbCrLf & "EE Name: " & emp & " " & vbCrLf
ActiveCell.Value = cellFill
Call RealTimeTracker
End If
End If
Unload Me
Sheet1.Activate
End Sub
@Leah,您的代码确实有效。如果您尝试这样做(改编自您的代码),您会发现它有效:
Sub test()
currtime = Time()
waitTill = Now() + TimeValue("00:00:05")
While Now() < waitTill
DoEvents
Wend
dur = Format(Now() - currtime, "hh:mm:ss")
MsgBox (dur)
End Sub
您遇到的问题是 dur
太接近 currtime
,因此,实际经过的时间是 0。
您可以尝试将其放在代码的较低位置,如下所示:
If NoFill = True Then
cellFill = ""
ElseIf NoFill = False Then
With Sheet5
'...
.Range("I2").Value = Format(Now() - currtime, "hh:mm:ss")
'...
End With
'...
End If
但是,我认为这不会有什么不同,因为代码似乎没有做任何事情 'complicated' 足以花费超过第二.
在我的程序中,用户填写一个用户表单,该表单自动生成在另一个 excel sheet 上填写的信息和时间。我想使用 VBA 来计算从表单的原始输入时间到当前时间的持续时间。我不明白为什么我的代码不起作用。当我尝试使用它时,持续时间一直为零。我不确定我需要从“Now()”中减去什么才能完成这项工作并给我一个实际值。
代码如下:
Private Sub cmdOkUpdate_Click()
Dim length As Date
length = Format(Now(), "hh:mm:ss")
strBOL = txtBOL.Value
strID = txtID.Value
details = txtDet.Value
opt = lbxOption.Value
currtime = time()
today = Format(Now(), "MM/DD/YYYY")
emp = TextBox1.Value
dur = Format(Now() - currtime, "hh:mm:ss")
If NoFill = True Then
cellFill = ""
ElseIf NoFill = False Then
With Sheet5
.Range("A1").Value = "Time"
.Range("B1").Value = "Date"
.Range("C1").Value = "Location"
.Range("D1").Value = "Category"
.Range("E1").Value = "BOL"
.Range("f1").Value = "Trailer #"
.Range("g1").Value = "Details"
.Range("H1").Value = "EE Name"
.Range("I1").Value = "Duration"
.Range("A2").EntireRow.Insert
.Range("A2").Value = currtime
.Range("B2").Value = today
.Range("C2").Value = spot
.Range("D2").Value = opt
.Range("E2").Value = strBOL
.Range("F2").Value = strID
.Range("G2").Value = details
.Range("H2").Value = emp
.Range("I2").Value = dur
.Columns("A:I").AutoFit
End With
If Not IsEmpty(opt) Then
cellFill = opt & " " & vbCrLf & "BOL (last 5 digits): " & strBOL & " " & vbCrLf & "Trailer # " & strID & " " & vbCrLf & "Details: " & details & " " & vbCrLf & "EE Name: " & emp & " " & vbCrLf
ActiveCell.Value = cellFill
Call RealTimeTracker
End If
End If
Unload Me
Sheet1.Activate
End Sub
@Leah,您的代码确实有效。如果您尝试这样做(改编自您的代码),您会发现它有效:
Sub test()
currtime = Time()
waitTill = Now() + TimeValue("00:00:05")
While Now() < waitTill
DoEvents
Wend
dur = Format(Now() - currtime, "hh:mm:ss")
MsgBox (dur)
End Sub
您遇到的问题是 dur
太接近 currtime
,因此,实际经过的时间是 0。
您可以尝试将其放在代码的较低位置,如下所示:
If NoFill = True Then
cellFill = ""
ElseIf NoFill = False Then
With Sheet5
'...
.Range("I2").Value = Format(Now() - currtime, "hh:mm:ss")
'...
End With
'...
End If
但是,我认为这不会有什么不同,因为代码似乎没有做任何事情 'complicated' 足以花费超过第二.