Excel VBA OnTime不到1秒,而不会变得无响应 [英] Excel VBA OnTime for less than 1 second without becoming Unresponsive
问题描述
当我使用像
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屋!