- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 VBA 代码,它会在需要离开办公室时通知我。
它会从工作簿工作表中获取时间提示我,但当我更新上类时间、午餐时间等时,该值会发生变化。
我创建了一个在某些单元格发生变化时触发的代码,问题是单元格在到达我应该离开的实际时间之前发生了多次变化。因此,我收到的不是一个通知,而是多个通知。
基本上同一个宏会运行多次。当我更改单元格时,如果该宏正在运行,它实际上应该停止它,然后重新启动我的宏。
我用谷歌搜索过,但没有任何帮助。
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
设为公共(public)变量,您可以使用它来引用之前安排的完全相同的过程,并使用 Schedule:=False
关闭该过程。
请尝试这个,通常我是最后一个使用 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 工作簿打开,则该工作簿将在计划时间自动打开并执行代码,除非该任务在关闭工作簿时也被终止。如果您想阻止它,应将以下代码放入 ThisWorkbook
对象的代码中:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime notificationTime, "NotifyMe", Schedule:=False
End Sub
关于excel - 如何阻止 Application.OnTime 自身重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524743/
我写下了下面的VBA代码,每5秒执行一次,但它不起作用。 Option Explicit Public RunWhen As Double Public Const cRunIntervalSecon
我一直在尝试使用 Apache Beam 的计时器,但无法触发它们。 据我所知,您可以在 DoFn 中按以下方式定义计时器。 @TimerId("expiry") private final Time
我最近一直在处理一个让我发疯的问题,因为它只是在部署到 Dataflow 中后才会发生,但从来没有在本地运行过,一切都完美无缺。仅供引用,我正在使用 Apache Beam 2.9.0。 我正在定义一
我有一个分配给 StopRecordingData 子的按钮来取消两个子,但它没有。时间表 False 似乎并没有取消 que 中的预定潜艇。 Dim NextTime As Double Sub R
我遇到过解释“要取消挂起的 OnTime 事件,您必须提供它计划运行的确切时间”的帖子。 我应该提供事件第一次运行的时间,还是应该提供下一次触发事件的时间? 我已经尝试了两个版本的 StopTimer
我正在尝试取消 ThisWorkbook 模块的 Workbook_Close 例程中的计时器。谁能解释以下行为?: 手动关闭工作簿 Application.OnTime 按预期运行并取消计时器。 如
我正在使用 Application.Ontime 命令在一段时间不活动(10 分钟)后自动关闭电子表格。 以下代码似乎可以正常工作,但是,如果您自己手动关闭工作表,工作簿似乎仍然在后台处于事件状态,并
基本上我有一个程序,它有 6 个通过串行端口发送值的 slider ,我使用计时器在用户更改这些值时动态重新发送这些值。我遇到的问题是我的计时器调用的 6 个函数的行为很奇怪,第一个函数调用正常工作,
我的应用程序中有一个 TTimer,每 2 秒触发一次并调用我的事件处理程序 HandleTimerEvent()。 HandleTimerEvent() 函数会修改共享资源,并且在返回之前可能需要
我需要每天在特定时间运行我的 Excel VBA。我用谷歌搜索了 ontime 方法,但是我没有从中得到全面的理解。我会提出这个来消除我对 ontime 方法的困惑。 在运行之前,包含的 Excel
我正在开发 Excel 2010 的数据采集前端。我无法弄清楚将多个局部变量参数传递给 Application.OnTime 的语法。 http://markrowlinson.co.uk/artic
vba 函数中字符串中可以使用的最大字符数为 255。我正在尝试运行这个函数 Var1= 1 Var2= 2 . . . Var256 =256 RunMacros= "'Tims_pet_Robot
我已经阅读了一些关于如何停止 VBA Application.OnTime 的问题/线程(例如 this 和 this )程序,但我就是无法停止! 我用它每 x 秒提取一些数据。我明白当我调用 OnT
这里遇到了一些麻烦,现在我只是想让一个图像在屏幕上移动,变量和定位是有效的,但由于某种原因,OnTimer 从未被调用,我在其中放置了一个断点,但它从未被调用到达。没有错误,所以我假设所有方法和变量都
我有一个使用 ON_WM_TIMER 的 MFC 应用程序。 前 1-2 分钟,我用 启动计时器 m_nIDEvent = SetTimer( 234, 500, NULL ); 这工作得很好,但在运
我不确定我是否在做一些没有记录的事情。我创建了自己的派生自 CListCtrl 的类然后覆盖其中的 OnTimer 处理程序: void CListCtrl2::OnTimer(UINT_PTR nI
通常,当人们收到“'onTimer' 没有重载匹配委托(delegate) 'ElapsedEventHandler'”错误时,这是由于没有在每次计时器结束时运行的 void 中包含 Elapse
我使用 application.ontime 在 Excel 中的 VBA 中编写了一个计时器当到达结束时间时,它会发出声音并给出一个消息框。 当时间在同一天晚些时候时,这很好用,但是当它到第二天时,
我正在尝试使用类似于此的代码在宏中实现递归功能-: Dim showTime As Boolean Sub RunClock() Range("A1", "A1").Valu
我想制作一个在后台循环运行的程序,并在可刷新的查询上运行,而不会同时挂起 excel,当发生错误时,它会显示一条消息。 因此,对我有用的唯一想法是使用 Application.Ontime 安排一个过
我是一名优秀的程序员,十分优秀!