Excel VBA 中用户表单上的计时器
我有一些旧的 Excel VBA 代码,我想在其中定期运行任务。如果我使用 VB6,我会使用计时器控件。
我找到了 Application.OnTime()方法,它适用于在 Excel 工作表中运行的代码,但我无法使其在用户表单中运行。该方法永远不会被调用。
如何使 Application.OnTime() 调用用户窗体中的方法,或者是否有其他方法来安排代码在 VBA 中运行?
I've got some old Excel VBA code where I want to run a task at regular intervals. If I were using VB6, I would have used a timer control.
I found the Application.OnTime() method, and it works well for code that's running in an Excel worksheet, but I can't make it work in a user form. The method never gets called.
How can I make Application.OnTime() call a method in a user form, or are there other ways to schedule code to run in VBA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了解决这个问题的方法。如果您在模块中编写的方法仅调用用户表单中的方法,则可以使用 Application.OnTime() 调度该模块方法。
有点拼凑,但除非有人有更好的建议,否则它会起作用。
这是一个例子:
I found a workaround for this. If you write a method in a module that just calls a method in your user form, then you can schedule the module method using Application.OnTime().
Kind of a kludge, but it'll do unless somebody has a better suggestion.
Here's an example:
我需要一个可见的倒计时器,它可以保持在其他窗口之上,并且无论是更改工作簿还是最小化 Excel 窗口,都能顺利运行。因此,我出于自己的目的改编了 @don-kirkby 的创意上面的代码,并认为我会分享结果。< br>
下面的代码需要创建一个模块和一个用户表单,如评论中所述,或者您可以下载本答案底部的
.xlsm
。我使用了 Windows 计时器 API 来实现更准确、更流畅的倒计时(并且还可自定义至约 100 毫秒计时器分辨率,具体取决于您的处理器甚至还有“滴答声”声音
。模块并将其另存为
modUserFormTimer
添加两个 表单控制命令按钮到工作表,标记为启动计时器和停止计时器和分配过程btnStartTimer_Click
和btnStopTimer_Click.
接下来,创建一个用户表单,将其另存为
frmTimer
。添加一个名为txtCountdown
的文本框。将属性ShowModal
设置为False
。将以下内容粘贴到表单的代码窗口中:在此处下载演示
.xksm.
。有多种方法可以定制或适应特定需求。我将使用它来计算并在屏幕一角显示来自流行问答网站的实时统计数据...注意,由于它包含 VBA 宏,因此文件可能会触发您的病毒扫描程序(与使用 VBA 的任何其他非本地文件一样)。如果您担心,请不要下载,而是使用提供的信息自行构建。)
I needed a visible countdown timer that could stay on top of other windows and run smoothly whether making changes to the workbook, or minimizing the Excel window. So, I adapted the @don-kirkby's creative code above for my own purposes and figured I'd share the result.
The code below requires creation of a module and a userform as noted in the comments, or you can download the
.xlsm
at the bottom of this answer.I used the Windows Timer API for more accurate and smooth countdown (and also customizable down to ~100 millisecond timer resolution, depending on your processor. There's even a "tick tock" sound. ⏰
Insert a new module and save it as
modUserFormTimer
. Add two form control command buttons to the worksheet, labelled Start Timer and Stop Timer and assigned proceduresbtnStartTimer_Click
andbtnStopTimer_Click
.Next, create a userform, save it as
frmTimer
. Add a text box namedtxtCountdown
. Set propertyShowModal
toFalse
. Paste the following into the form's code window:Download the demo
.xksm.
here. There are numerous ways this can be customized or adapted to specific needs. I'm going to use it to calculated and display real time statistics from a popular Q&A site in the corner of my screen...Note that, since it contains VBA macro's, the file could may set off your virus scanner (as with any other non-local file with VBA). If you're concerned, don't download, and instead build it yourself with the information provided.)
将所有代码移至“计时器”模块怎么样?
现在您可以从主窗体调用:
为了防止错误,添加:
它将触发:
How about moving all the code to a 'Timer' module.
Now you can call from the mainform:
To prevent errors, add:
Wich will trigger:
感谢用户1575005!
使用模块中的代码来设置 Timer() 进程:
Thanks to user1575005 !!
Used the code in a Module to setup a Timer() process: