在 VBA 中是否有等效于 Thread.Sleep() 的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/469347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an equivalent to Thread.Sleep() in VBA
提问by Johnno Nolan
Is there an equivalent to Thread.Sleep()in Access VBA?
Thread.Sleep()在 Access VBA 中是否有等效项?
回答by Otávio Décio
Declare Sub Sleep Lib "kernel32" Alias "Sleep" _
(ByVal dwMilliseconds As Long)
Use the following syntax to call the Sleep function:
使用以下语法调用 Sleep 函数:
Sub Sleep()
Sleep 1000 'Implements a 1 second delay
End Sub
回答by DontFretBrett
Another way without using kernel32:
另一种不使用 kernel32 的方法:
Dim started As Single: started = Timer
Do: DoEvents: Loop Until Timer - started >= 1
回答by Zorba Eisenhower
A couple of amendments are required to get the code to work. The code below is the corrected version.
需要进行一些修改才能使代码正常工作。下面的代码是更正后的版本。
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Sub SleepVBA()
Sleep 1000 'Implements a 1 second delay
End Sub
回答by The Data Brewer
All of the rest of the methods to make Excel wait result in Excel becoming completely unresponsive. The solution to make Excel wait while ensuring a responsive UI is to call this wait Sub with the number of seconds to wait.
使 Excel 等待的所有其他方法都会导致 Excel 完全无响应。在确保响应式 UI 的同时让 Excel 等待的解决方案是使用等待的秒数调用此 wait Sub。
Sub Wait(seconds As Integer)
Dim now As Long
now = Timer()
Do
DoEvents
Loop While (Timer < now + seconds)
End Sub
回答by user3298002
It is possible to use the Excel Wait() procedure from Access VBA.
可以使用 Access VBA 中的 Excel Wait() 过程。
The first step is to ensure that the Excel library is referenced from your project.
第一步是确保从您的项目中引用 Excel 库。
When that's done the following code will work to wait for ten seconds :
完成后,以下代码将等待十秒钟:
Call Excel.Application.Wait(Time:=DateAdd("s",10,Now()))
回答by Tony L.
If you use Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long), you may get this error in an object module.
如果使用Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long),则可能会在对象模块中收到此错误。
If so, you can declare it as private:
如果是这样,您可以将其声明为私有:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
回答by Gaffi
I use this in Excel and it works great:
我在 Excel 中使用它并且效果很好:
Application.Wait DateAdd("s", 1, Now())
DateAdd() is a function that set a time, relative to Now()(in this case - you can use other values as your argument), "s"is the time measure (seconds in this case), and the increment is 1. So here, the function call is telling the application to wait 1 second.
DateAdd() 是一个设置时间的函数,相对于Now()(在这种情况下 - 您可以使用其他值作为参数),"s"是时间度量(在这种情况下为秒),增量为 1。所以在这里,函数call 告诉应用程序等待 1 秒。
See alsofor more detail about the use of the DateAddfunction.
回答by sebastien leblanc
Adding
添加
Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
somehow created additional problems somewhere else in my code. I ended up using this function that I found on an other forum and tweeked a bit:
以某种方式在我的代码中的其他地方产生了额外的问题。我最终使用了我在其他论坛上找到的这个功能,并做了一些调整:
Function WaitTime(n As Double)
'Function that wait an amount of time n in seconds
TWait = Time
TWait = DateAdd("s", n, TWait)
Do Until TNow >= TWait
TNow = Time
Loop
End Function
hope this helps :)
希望这可以帮助 :)


