【问题标题】:excel timer macro not working, "macro can't be found or macros not enabled"excel计时器宏不起作用,“找不到宏或未启用宏”
【发布时间】:2016-12-18 06:46:16
【问题描述】:

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

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 方法根本无法识别,正如错误所说: 该工作簿中的宏可能不可用,或者所有宏都可能被禁用。显然情况并非如此,因为我的所有其他宏都可以工作,并且子例程就在那里。你有什么建议?

【问题讨论】:

标签: vba excel macros


【解决方案1】:

将您的代码移出工作表代码模块,并将其放入普通代码模块中。

或者,您可以通过将其完全限定为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

【讨论】:

  • sheet1.nexTime 工作完美。感谢您的时间。我对 vba 很陌生,所以我也会尝试你的建议。
猜你喜欢
  • 2017-02-14
  • 2018-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多