尝试在VBA中创建定时器。我的意图是能够在其他函数中调用TimerStart
, TimerLaps
和TimerStop
,以估计代码块需要多长时间才能完成。但是我有问题,我的全局变量是空的。
Private starttime As Double
Sub TimerStart()
starttime = Timer
End Sub
Function TimerLaps() As Double
TimerLaps = Timer - starttime
End Function
Function TimerStop() As Double
i = starttime
starttime = 0
TimerStop = Round(Timer - i, 2)
End Function
Sub test()
Call TimerStart
MsgBox TimerStop()
End Sub
谢谢你们了!
有两种用法:
Global starttime As Double
和
Public starttime As Double
用其中一个代替Private
。主要区别在于:
Global只能在标准模块中使用,而Public可以在所有上下文中使用(模块,类,控件,表单等)
Global来自旧版本的VB,可能是为了向后兼容而保留的,但已完全被Public所取代。
尝试:
Global starttime As Double
在模块的顶部(代替Private starttime As Double
)
编辑
Global starttime As Double
Sub test()
Call TimerStart
Application.Wait (Now + #12:00:02 AM#)
MsgBox TimerStop()
End Sub
Sub TimerStart()
starttime = Timer
End Sub
Function TimerStop() As Double
Dim i As Double
i = starttime
starttime = 0
TimerStop = Round(Timer - i, 2)
End Function