使用Application.OnTime时停止宏重复

时间:2018-12-31 22:31:08

标签: excel vba

通过使用以下代码 RefreshData ,我每10秒运行一次 mg 宏。

我无法停止分配给正方形的 stoprefresh

Sub RefreshData()
    Application.OnTime Now + TimeValue("00:00:10"), "mg", , True
End Sub

Sub stoprefresh()
    On Error Resume Next
    Application.OnTime Now + TimeValue("00:00:10"), "mg", , False
End Sub

Sub mg()

    ActiveSheet.Cells(ActiveSheet.Cells.Rows.Count, 1).End(xlUp).Offset(1) = "Running"

    Call RefreshData

End Sub

1 个答案:

答案 0 :(得分:1)

尝试此代码

Option Explicit

Dim iTimerSet As Double
Sub RefreshData()
    iTimerSet = Now + TimeValue("00:00:10")
    Application.OnTime iTimerSet, "mg", , True
End Sub
Sub stoprefresh()
    'On Error Resume Next
    Application.OnTime iTimerSet, "mg", , False
End Sub

Sub mg()
    ActiveSheet.Cells(ActiveSheet.Cells.Rows.Count, 1).End(xlUp).Offset(1) = "Running"
    Call RefreshData
End Sub
  

Regular_Expressions Guide by Mozilla   可以取消已计划运行的过程,但是您需要了解   确切的日期和时间。取消预定   步骤,您必须知道预定的“最早时间”。   完全相同的语法,只不过您将schedule参数设置为   假。这告诉应用程序取消计划。

相关问题