VB中怎样终止正在运行的timer程序还有在excel VBA中怎样终止正在快速运行的vba ontimee循

查看: 7584|回复: 6
[求助]有没有什么快捷键能让正在运行的VB程序中止?
有没有什么快捷键能让正在运行的VB程序中止?
CTRL+BREAK
一次不行就多按几次。
CTRL+BRACK快捷键
hwc2ycy 发表于
CTRL+BREAK
是不是PAUSE BREAK?我都连续按了1分钟了
你这种情况应该是EXCEL本身就没有响应了,很难暂停,只有耐心的等。
我也碰到过,程序运行很吃力,在任务管理器中已经可以看到EXCEL,VBE是无响应状态。
放几分钟,等程序运行完就好了。
Powered byPowered by
本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任! & & 本站特聘法律顾问:徐怀玉律师 李志群律师查看: 2742|回复: 11
取消OnTime失败,求解释
我在Workbook_Open及Workbook_BeforeClose中各添置了Ontime及取消Ontime的语句,但未执行该语句关闭Excel后,仍会自动打开Excel文件,说明取消语句使用失败了,求解!!!
设置语句如下:
Public RunWhen As Double
Private sub Workbook_Open()
RunWhen= Now + Timeserial(0,0,60)
Application.OnTime RunWhen, &过程名&
Private sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.OnTime RunWhen, &过程名&,,False
实不知问题出在哪里,望大侠不吝赐教
Application.OnTime 方法
安排一个过程在将来的特定时间运行(既可以是具体指定的某个时间,也可以是指定的一段时间之后)。
表达式.OnTime(EarliestTime, Procedure, LatestTime, Schedule)
表达式& &一个代表 Application 对象的变量。
名称 必选/可选 数据类型 说明
EarliestTime 必选 Variant 希望此过程运行的时间。
Procedure 必选 String 要运行的过程名。
LatestTime 可选 Variant 过程开始运行的最晚时间。例如,如果 LatestTime 参数设置为 EarliestTime + 30,且当到达 EarliestTime 时间时,由于其他过程处于运行状态而导致 Microsoft Excel 不能处于“就绪”、“复制”、“剪切”或“查找”模式,则 Microsoft Excel 将等待 30 秒让第一个过程先完成。如果 Microsoft Excel 不能在 30 秒内回到“就绪”模式,则不运行此过程。如果省略该参数,Microsoft Excel 将一直等待到可以运行该过程为止。
Schedule 可选 Variant 如果为 True,则预定一个新的 OnTime 过程。如果为 False,则清除先前设置的过程。默认值为 True。
使用 Now + TimeValue(time) 可安排经过一段时间(从现在开始计时)之后运行某个过程。使用 TimeValue(time) 可安排某个过程只运行指定的时间。
本示例设置从现在开始 15 秒后运行 my_Procedure。
Visual Basic for Applications
Application.OnTime Now + TimeValue(&00:00:15&), &my_Procedure&
本示例设置 my_Procedure 在下午 5 点开始运行。
Visual Basic for Applications
Application.OnTime TimeValue(&17:00:00&), &my_Procedure&
本示例撤消前一个示例对 OnTime 的设置。
Visual Basic for Applications
Application.OnTime EarliestTime:=TimeValue(&17:00:00&), _
& & Procedure:=&my_Procedure&, Schedule:=False
本帖最后由 hwc2ycy 于
15:21 编辑
你的 RunWhen是否放在标准模块中了?
另外,如果已经触发过的代码,再用ONTIME取消是会报错的。
你可以试试就知道了。
是在标准模块中,该代码未被触发,触发过的代码报错问题我已解决
(20.15 KB, 下载次数: 82)
15:39 上传
点击文件名下载附件
我做了个范例,你试试就知道了。
hwc2ycy 发表于
我做了个范例,你试试就知道了。
这个确实行,但我没有明白我的程序问题出在哪里。目前我程序OnTime定时5分钟,打开Excel后立即关闭,5分钟后仍会要求打开Excel执行相应程序。我再借鉴一下您的程序,再仔细研究一下,谢谢
hwc2ycy 发表于
我做了个范例,你试试就知道了。
我的程序中完全参照您范例中的格式,但还是没能终止该OnTime;另外,您程序中 Sub Teset()有何作用,我没能看懂
你是不是没有打开强制声明变量?
你把你的附件发上来。
teset没有任何意义。
你在取消时,要求时间,过程名,还有就是ONTIME并没有触发,这三个条件都满足了才能取消成功的。
把你的附件传上来,就知道问题在那里了!!
Powered byexcel vba Application.OnTime - Stack Overflow
to customize your list.
Announcing Stack Overflow Documentation
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
I asked a question about this last week
I posted my code as requested but no answer.
Anyway, I have some more specific questions.
When resetting the timer, what is the purpose of the Procedure argument?
It doesn't make sense to have a call-back with Schedule:=False.
What happens if the call-back function is no longer in scope for example? Is the timer still reset properly?
Is this why most of the examples I've seen (including Chip Pearson - unfortunately, for some reason I'm not allowed to include the link) proceed the OnTime call with On Error Resume Next
And, sorry to ask again, but why is this done?
My understanding is that the the timer is registered by the EarliestTime argument and that this is the "serial number" for the timer that uniquely identifies it.
Is the Procedure argument also included in the registration and subsequent identification process as well?
What happens if the call-back procedure is contained in an object that no longer exists when the timer fires?
Or more generally, what happens if there is some error when the timer fires and tries to execute the call-back procedure?
If the LatestTime argument is included, what happens after that time if the timer has not been able to fire?
Does Windows erase the timer completely?
23.8k1677113
3,93831333
My understanding is that the the timer is registered by the EarliestTime argument and that this is the "serial number" for the timer that uniquely identifies it.
Not sure where you read this? Not that it's a smart thing to do, but you could in principle schedule two or more OnTime runs with the same EarliestTime. So that doesn't make it unique. The combination of EarliestTime and Procedure makes it unique.
When resetting the timer, what is the purpose of the Procedure argument?
Not sure what you mean by "resetting" do you mean calling OnTime with Schedule := True or Schedule := False?
Either way, how else is it supposed to know which procedure to run (or which procedure run to cancel) if you don't specify the Procedure argument? That's what it's for.
I suggest you try it out and convince yourself.
What happens if the call-back procedure is contained in an object that no longer exists when the timer fires? Or more generally, what happens if there is some error when the timer fires and tries to execute the call-back procedure?
Well, have you tried it to find out for yourself? Nothing mysterious happens. You get an error message as usual.
If the LatestTime argument is included, what happens after that time if the timer has not been able to fire? Does Windows erase the timer completely?
I guess you could put it that way. The procedure won't be run. See VBA help:
For example, if LatestTime is set to EarliestTime + 30 and Microsoft Excel is not in Ready, Copy, Cut, or Find mode at EarliestTime because another procedure is running, Microsoft Excel will wait 30 seconds for the first procedure to complete. If Microsoft Excel is not in Ready mode within 30 seconds, the procedure won’t be run.
23.8k1677113
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled  [摘 要]因为Excel集数据库和程序设计于一体,令广大VBA编程爱好者前赴后继地奔走在Excel考试系统路上,定时器" />
免费阅读期刊
论文发表、论文指导
周一至周五
9:00&22:00
深入探讨Excel Application对象OnTime方法的死穴
2016年2期目录
&&&&&&本期共收录文章20篇
  [摘 要]因为Excel集数据库和程序设计于一体,令广大VBA编程爱好者前赴后继地奔走在Excel考试系统路上,定时器作为考试系统的基本要求,而Excel却没有Timer,使爱好者乱了方寸,幸好Application对象OnTime方法带来了一线生机,但它又无法感知系统时间被修改的弊端,成了Excel考试系统的死穴,又让爱好者垂头丧气。 中国论文网 /1/view-7278247.htm  [关键词]探讨 Excel Application对象 OnTime方法   中图分类号:TP311.52 文献标识码:A 文章编号:X(1-01   Excel考试系统的重点和难点都是定时器的实现,但定时器作为任何考试系统的基本要求,又无法回避,怎样彻底实现Excel考试系统的定时器功能呢?   一、实现定时器的基本方法   虽然Excel VBA没有提供定时器,但可以通过Application对象的OnTime方法实现简单的定时器功能,OnTime方法能够安排一个过程在将来的特定时间运行,既可以是具体指定的某个时间,也可以是指定的一段时间之后。   语法如下:Application.OnTime EarliestTime,Procedure,LatestTime,Schedule   在OnTime方法中递归调用Procedure过程,就可实现定时器。   二、致命的死穴   尽管OnTime方法可以具体指定的某个时间和指定的一段时间之后两种方式执行Procedure,但本质上都是具体指定的某个时间,因此一旦修改系统时间,不管是提前或延后时间,让其错过指定时刻,或让指定时刻延后数小时或更长,导致OnTime方法失效,从而失去定时器的功能,成为Excel考试系统的死穴。   三、解决方案   许多编程爱好者都在苦苦探索一旦修改系统时间,立刻让Excel感知系统时间被修改了,从而调整定时器方案,其中想利用类似lostfocus和getfocus事件的Workbook_Activate()、Workbook_SheetActivate()、Workbook_WindowActivate()等事件的探索者,均以失败而告终,因为WindowActivate等事件都是在有两个以上workbook打开时(也就是多个excel文件),互相之间切换起作用,而不是workbook和其他程序窗口间切换。同理,WindowDeactivate也得是在多个workbook时起作用。   经过多次探索,终于发现Worksheet_Change()或Workbook_SheetChange()可担当此大任,两个事件都在编辑单元格后触发事件,类似TextChange事件,因为一般而言,Excel随时随地都处于编辑状态,所以时时刻刻都会触发Worksheet_Change()或Workbook_SheetChange()事件,只要在事件中获取系统时间,并比较前后时间的变化,即可判断系统时间是否被修改。尤其是系统时间被修改为以前的时间,更容易感知,因为时间毕竟不能倒流。   四、具体实现方法   首先定义两个全局变量Public pass As Date,Public nowtime As Date,在Workbook_Open()事件中获取系统时间,启动定时器过程displaytime()。   Private Sub Workbook_Open()   nowtime = Now()   pass = nowtime - TimeValue("01:00:00") '防恶意修改系统时间   displaytime   End Sub   而在定时器中递归调用过程displaytime()。   Public Sub displaytime()   nexttime = Now + TimeValue("00:00:01")   If Range("D2").Value > TimeValue("00:00:00") Then   Application.OnTime nexttime, "ThisWorkbook.displaytime", , True   End If   End Sub   在Worksheet_Change()或Workbook_SheetChange()事件中利用DateDiff函数比较pass和nowtime两个变量,当nowtime比pass还早,说明修改了系统时间,提示并作关闭Excel处理。   Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)   nowtime = Now()   If DateDiff("s", pass, nowtime) < 0 Then '修改系统时间,按提交处理   Response = MsgBox(" 恶意修改系统时间 ", 0, "提示")   ActiveWorkbook.Save   Application.Quit   Else   pass = nowtime '正常,时间在一分一秒过去   End If   End Sub   五、定时器在考试系统中的注意事项   在考试系统中,发现修改系统时间进行异常处理时,一般都要在Workbook_SheetChange()中记录考试状态和退出考试的原因,并存放在Excel文档中,此时又会触发Workbook_SheetChange()形成递归调用,而又没有递归的退出机制,导致堆栈空间溢出,Excel出现1004号错误,使整个考试系统崩溃,那么又怎样避免Workbook_SheetChange()形成递归调用呢?可在事件处理中使用Application.EnableEvents=False禁止触发事件,而处理完成后Application.EnableEvents=True重新启动触发事件。   至此,Excel考试系统的重点和难点——定时器的实现方法探讨完毕,整个考试系统彻底健壮起来了,真正完成了Excel考试系统的开发工作。   参考文献   [1] 张志群,EXCEL单元格自定义数字格式探讨,微型电脑应用,2008,24   [2] 甘伟明,用Excel构建计算机等级考试自动改卷评分系统,电脑学习 2010,2
转载请注明来源。原文地址:
【xzbu】郑重声明:本网站资源、信息来源于网络,完全免费共享,仅供学习和研究使用,版权和著作权归原作者所有,如有不愿意被转载的情况,请通知我们删除已转载的信息。
xzbu发布此信息目的在于传播更多信息,与本网站立场无关。xzbu不保证该信息(包括但不限于文字、数据及图表)准确性、真实性、完整性等。

参考资料

 

随机推荐