macros重叠本身

我正在写一个VBA代码,通知我什么时候离开办公室。 它得到了应该从工作簿提示我的时间,但是当我更新我工作的时间,午餐的时间等等时,这个值会改变。我创build了一个代码,当一些单元格发生变化时,问题在于细胞在到达我应该离开的实际时间之前多次改变。 所以,而不是得到一个通知,我得到了他们的多个。 基本上相同的macros多次运行。 当我改变一个单元格时,它应该实际上停止这个macros,如果它正在运行,并重新启动我的macros。 可以做到吗? 我GOOGLE了,没有什么帮助。

Sub NotifyMe() 'Declare Variables Dim notificationStr, leaveStr As String Dim notificationTime As Date Dim leaveTime As Date 'Defines now Time h = Hour(Now()) m = Minute(Now()) s = Second(Now()) nowtime = TimeSerial(h, m, s) 'Defines the time it will prompt me leaveTime = Cells(5, 2).Value notificationTime = Cells(5, 2).Value - Cells(6, 2).Value 'Creates a string to be presented in the MsgBox notificationStr = Format(notificationTime, "Short Time") leaveStr = Format(leaveTime, "Short Time") nowStr = Format(nowtime, "short time") ' If it's passed the time, it will notify me If nowtime >= notificationTime Then Beep a = MsgBox("Agora sao " & nowStr & ". E voce tem que sair as " & leaveStr, vbExclamation, "Nao se Atase!") Else 'Schedules the macro to run at the notificationTime Application.OnTime EarliestTime:=notificationTime, Procedure:="NotifyMe", Schedule:=True End If End Sub 'Runs NotifyMe everytime a keycell is changed Private Sub Worksheet_Change(ByVal Target As Range) Dim keyCells As Range Set keyCells = Range("B1:B8") If Not Application.Intersect(keyCells, Range(Target.Address)) Is Nothing Then NotifyMe End If End Sub 

您可以使用此模式结束预定的Application.OnTime事件:

 Public notificationTime As Date Application.OnTime notificationTime, "NotifyMe", Schedule:=False 

通过执行notificationTime公共variables,您可以使用它来引用完全相同的过程计划之前,并使用Schedule:=Falseclosures过程。

请试试这个,通常我是最后一个使用On Error Resume Next的人,但我认为这是最简单和最可靠的方法。

 Option Explicit Public notificationTime As Date Sub NotifyMe() 'Declare Variables Dim notificationStr, leaveStr As String, a As String Dim nowtime As Date, leaveTime As Date, nowStr As Date Dim h As Long, m As Long, s As Long On Error Resume Next Application.OnTime notificationTime, "NotifyMe", Schedule:=False On Error GoTo 0 'Defines now Time h = Hour(Now()) m = Minute(Now()) s = Second(Now()) nowtime = TimeSerial(h, m, s) 'Defines the time it will prompt me leaveTime = Cells(5, 2).Value notificationTime = Cells(5, 2).Value - Cells(6, 2).Value 'Creates a string to be presented in the MsgBox notificationStr = Format(notificationTime, "Short Time") leaveStr = Format(leaveTime, "Short Time") nowStr = Format(nowtime, "Short Time") ' If it's passed the time, it will notify me If nowtime >= notificationTime Then Beep a = MsgBox("Agora sao " & nowStr & ". E voce tem que sair as " & leaveStr, vbExclamation, "Nao se Atase!") Else 'Schedules the macro to run at the notificationTime Application.OnTime EarliestTime:=notificationTime, Procedure:="NotifyMe", Schedule:=True End If End Sub 

同样,在退出此Excel工作簿时,如果计划任务,但其他Excel工作簿已打开,则工作簿将在计划时间自动打开并执行代码,除非在closures工作簿时也终止该任务。 下面的代码应该放在ThisWorkbook对象的代码中,如果你想阻止它:

 Private Sub Workbook_BeforeClose(Cancel As Boolean) On Error Resume Next Application.OnTime notificationTime, "NotifyMe", Schedule:=False End Sub 

在开始时使用Application.EnableEvents = false在macros的末尾使用Application.EnableEvents = true。 当你处于macros观状态时,它会压制进一步的事件,一旦完成,就会启用它们。