【发布时间】:2016-02-05 23:08:42
【问题描述】:
问题: 我已经对此进行了广泛的搜索,但似乎无法使其正常工作。当按下“StartBtn”时,我有一个计时器正在运行:
Dim StopTimer As Boolean
Dim SchdTime As Date
Dim Etime As Date
Dim currentcost As Integer
Const OneSec As Date = 1 / 86400#
Private Sub ResetBtn_Click()
StopTimer = True
Etime = 0
[TextBox21].Value = "00:00:00"
End Sub
Private Sub StartBtn_Click()
StopTimer = False
SchdTime = Now()
[TextBox21].Value = Format(Etime, "hh:mm:ss")
Application.OnTime SchdTime + OneSec, "Sheet1.NextTick"
End Sub
Private Sub StopBtn_Click()
StopTimer = True
Beep
End Sub
Sub NextTick()
If StopTimer Then
'Don't reschedule update
Else
[TextBox21].Value = Format(Etime, "hh:mm:ss")
SchdTime = SchdTime + OneSec
Application.OnTime SchdTime, "Sheet1.NextTick"
Etime = Etime + OneSec
End If
End Sub
然后在另一个单元格(例如 C16)中,我有一个手动输入的值,即每小时成本费率。我有第三个单元格,它通过 C16*当前计时器值计算总成本。
我想要做的是在单击“StartBtn”后每 5 秒记录一次当前时间和当前计算的成本在另一张表中。这是我开始的:
Sub increment()
Dim x As String
Dim n As Integer
Dim Recordnext As Date
n = 0
Record = [TextBox21].Value
Recordnext = [TextBox21].Value + OneSec
Range("B13").Value = Recordnext
Do Until IsEmpty(B4)
If [TextBox21].Value = Recordnext Then ActiveCell.Copy
Application.Goto(ActiveWorkbook.Sheets("Sheet2").Range("A1").Offset(1, 0))
ActiveSheet.Paste
Application.CutCopyMode = False
n = n + 1
Recordnext = [TextBox21].Value + 5 * (OneSec)
Exit Do
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
但它不起作用。任何帮助将不胜感激。
【问题讨论】:
-
您声明 '5 seconds' 但代码中的每个指示似乎都是 1 seconds。您的公共变量应该在模块代码表中,而不是工作表的代码表中。使用 *Option Explicit.