每当我尝试关闭包含Excel文件的宏时,它会关闭几秒钟,自行重新打开,然后出错,在下面的代码中给我一个未定义错误的对象 -
Option Explicit
Public TimerCell As Range
Public TimerValue As Range
Public StopTimer As Boolean
Public EndTime As Double
Public Sub TimeCounter()
If Not StopTimer Then
If EndTime - Now < 0 Then
EndTime = Now + TimerValue 'Specifically in this line
End If
Application.EnableEvents = False
TimerCell = EndTime - TimeValue(Now)
Application.EnableEvents = True
Application.OnTime Now + TimeSerial(0, 0, 1), "TimeCounter" 'the clock runs every second
End If
End Sub
我正在使用此代码在屏幕上运行第二个秒计时器,并在计时器完成后的10分钟内刷新所有宏。
它在上面标注的行中给了我一个Object variable not defined error
。任何帮助表示赞赏。
答案 0 :(得分:1)
导致错误是因为尚未设置TimerValue
(关闭excel时公共变量的实例被销毁)。您应该将值存储在xlVeryHidden
表单中,或者只添加一个条件来检查TimerValue
是Nothing
并对其进行相应处理(具体取决于您需要TimerValue
)