Excel VBA OnTime不到1秒,而不会变得无响应 [英] Excel VBA OnTime for less than 1 second without becoming Unresponsive

查看:908
本文介绍了Excel VBA OnTime不到1秒,而不会变得无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表,每100ms运行一个脚本。该脚本处理用户窗体上的图像,并用于对它们进行动画处理,而窗体继续接收用户输入(鼠标点击和按键)。直到用户窗体关闭为止。虽然Application.OnTime似乎工作最好,但它只能按时间值1秒或更长时间运行。



当我使用像

  Sub StartTimer()
Application.OnTime now +(TimeValue(00:00:01)/ 10),Timer
End Sub

Private Sub Timer()
TheUserForm.ScreenUpdate
Application.OnTime now +(TimeValue(00:00:01)/ 10),Timer
End Sub

并在用户窗体中调用StartTimer,Excel变得非常无响应,Timer被称为每秒更多的次数比它应该。



使用睡眠功能会导致程序变得无响应,尽管脚本以适当的时间间隔运行。 p>

是否有解决方法?感谢提前!

解决方案

OnTime 只能安排在增加1秒。当您尝试在1/10秒进行安排时,您实际上会在0秒内安排,即立即运行,耗费所有资源。



简短的回答,你不能使用 OnTime 每1/10秒运行一次事件。



还有其他方法,请参阅 CPearson 以调用Windows API

公开声明函数SetTimer Libuser32...


I have a userform which runs a script every 100ms. The script handles images on the userform and is used to animate them, while the form continues to receive user input (mouse clicks and key presses). This continues until the userform is closed. While Application.OnTime seems to work best, it only operates consistently on time values of 1 second or more.

When I use something like

Sub StartTimer()
    Application.OnTime now + (TimeValue("00:00:01") / 10), "Timer"
End Sub

Private Sub Timer()
    TheUserForm.ScreenUpdate
    Application.OnTime now + (TimeValue("00:00:01") / 10), "Timer"
End Sub

and call StartTimer in the userform, Excel becomes very unresponsive and "Timer" is called many more times per second than it should.

Using the Sleep function causes the program to become unresponsive too, although the script is run with the right interval.

Is there a workaround for this? Thanks in advance!

解决方案

OnTime can only be scheduled to run in increments of 1 second. When you attempt to schedule it at 1/10th second, you actually schedule at 0 seconds, ie it runs again immediately, consuming all resources.

Short answer, you cannot use OnTime to run an event every 1/10 second.

There are other ways, see CPearson for using a call to Windows API
Public Declare Function SetTimer Lib "user32" ...

这篇关于Excel VBA OnTime不到1秒,而不会变得无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆