我正在使用以下代码检查用户正在查看的只读工作簿是否已更新:
Function NewVersion() As Boolean
If a <> b Then Flag = True: Exit Function
Application.OnTime Now + TimeValue("00:00:05"), "NewVersion"
End Function
但是,每当我关闭工作簿时,它都会重新打开(大概是当NewVersion加载到内存中并设置为在5秒内运行?)
如何防止这种情况发生?
到目前为止,我尝试过各种各样的事情,但没有运气!
答案 0 :(得分:1)
创建一个公共变量,以便在使用Application.OnTime的Schedule参数时跟踪下一次宏应该触发和取消。
Public ScheduledTime As Date
Function NewVersion() As Boolean
ScheduledTime = Now + TimeValue("00:00:05")
Application.OnTime ScheduledTime, "NewVersion"
End Function
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime ScheduledTime, "NewVersion", Schedule:=False
End Sub