每当有另一个excel打开时,无法closures我的excel

每当我尝试closures包含Excel文件的macros时,它会closures几秒钟,重新打开它自己,然后出错,在下面的代码中给我一个对象未​​定义的错误 –

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分钟内刷新我所有的macros。

它给了我一个Object variable not defined error在上面注释的行。 任何帮助表示赞赏。

该错误是由于TimerValue尚未设置(公共variables的实例在Excelclosures时被销毁)而引起的。 您应该将值存储在xlVeryHidden表中,或者添加一个检查TimerValue是否为Nothing的条件,并相应地处理它(取决于您需要的TimerValue