我面臨幾個問題與我的OnTime
-method vba。代碼應該每30分鐘運行一次,但不知何故它會在兩次之間運行幾次。我假設這是因爲當我重置並重新運行代碼時,幾個OnTime
-方法可能正在運行。所以我想殺了準時的功能,但得到錯誤。這裏是我下面的代碼:申請準時
初始代碼:
Sub autorefresh()
dim timetorun
timetorun = Now + TimeSerial(0, 30, 0)
Application.OnTime timetorun, "manager" 'this runs the macro manager which
'runs several other macros and in
'the end calls this macro again in
'order to reset the timetorun counter
End Sub
我修改了下面的代碼,如果需要重置準時。
Public timetorun As Date 'so that timetorun can be used in both the functions
Sub autorefresh()
timetorun = Now + TimeSerial(0, 30, 0)
Application.OnTime timetorun, "manager"
End Sub
Sub killontime()
Application.OnTime earliesttime:=timetorun, procedure:="manager", schedule:=False '<~~this line gives the error
End Sub
如果公共'timetorun'已被'其他'功能改變,那麼它不能被用來取消第一個預定的宏。 – Jeeped
@Jeeped:它可能,但第一個問題是它在不同的範圍內被宣佈2次! ;) – R3uK
@ R3uK - 嗯...我已經推斷,它公開後,它被從子刪除,但你可以很好地解決這個問題;即如果它也在子中聲明,那麼一個是用來設置時間的,而不是公共的。 – Jeeped