如果您需要任何准确性,Excel可能不是做这件事的地方。但是,如果足够近就足够近,那么我会这样做。
在标准模块中:
代码语言:javascript复制Public gbStop As Boolean
Public gdtStart As Date
Sub UpdateTime()
Sheet1.Range("rngTimer").Value = Now - gdtStart
DoEvents
If Not gbStop Then
Application.OnTime Now + TimeSerial(0, 0, 1), "UpdateTime"
End If
End Sub在Sheet1的类模块中
代码语言:javascript复制Private Sub CommandButton1_Click()
gbStop = False
gdtStart = Now
Application.OnTime Now + TimeSerial(0, 0, 1), "UpdateTime"
End Sub
Private Sub CommandButton2_Click()
gbStop = True
End Sub单击Button1时,gbStop设置为false,开始时间记录在公共变量中,UpdateTime过程计划在1秒后运行。
一秒钟后,UpdateTime运行,名为rngTimer的单元格的值被更改,以显示从现在到Button1_Click中记录的开始之间经过的时间。如果gbStop仍然为False,则UpdateTime将自己调度为在另一秒内再次运行。
最后,按Button2,这会将公共变量gbStop设置为True。下次运行Updatetime时,它不会安排自己再次运行。