excel计时器macros不工作,“macros找不到或macros未启用”

所以我build立了这个计时器macros,不止一个YouTube用户指定了如何以完全相同的方式构build,但它仍然拒绝工作。

Sub StartTimer() Application.OnTime Now + TimeValue("00:00:01"), "nextTime" End Sub Sub nextTime() If Sheet1.Range("I1") = 0 Then Exit Sub End If Range("I1").Value = Range("I1").Value - TimeValue("00:00:01") StartTimer End Sub Sub StopTimer() Application.OnTime Now + TimeValue("00:00:01"), "nextTime", , False End Sub 

StartTimer子例程中的代码行出现问题。 nextTime方法根本不会被识别,因为Error说:macros可能在此工作簿中不可用,或者所有的macros可能被禁用。 这显然不是我的所有其他macros工作的情况下,子程序就在那里。 你有什么build议?

将代码移出工作表代码模块,并将其放入正常的代码模块中。

或者,您可以通过将其完全限定为Sheet1.nextTime (其中Sheet1需要是代码所在的工作表代码名称)来调用它,但是我认为将其放入项目级代码模块可能更好。

您还需要从nextTime子例程中删除End If

而且您需要修复StopTimer例程以传递正确的时间 – 即下一个事件运行的时间。

 Public nextRunTime As Date Sub StartTimer() nextRunTime = Now + TimeValue("00:00:01") Application.OnTime nextRunTime, "nextTime" End Sub Sub nextTime() If Sheet1.Range("I1") = 0 Then Exit Sub Range("I1").Value = Range("I1").Value - TimeValue("00:00:01") StartTimer End Sub Sub StopTimer() Application.OnTime nextRunTime, "nextTime", , False End Sub