Answers:
使用Wait方法:
Application.Wait Now + #0:00:01#
或(对于Excel 2010及更高版本):
Application.Wait Now + #12:00:01 AM#
Application.Wait(Now + #0:00:01#)
干杯!
Application.Wait (Now + TimeValue("0:00:01"))
Application.wait(now + 1e-5)
一秒钟,Application.wait(now + 0.5e-5)
半秒钟等等
将此添加到您的模块
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
或者,对于64位系统,请使用:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
像这样在宏中调用它:
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
Sleep (1000) ' delay 1 second
Loop
End Sub
Sleep()
您可以指定少于1秒的等待时间。Application.Wait
有时过于精细。
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
这对我来说完美无缺。在“直到”循环之前或之后插入任何代码。在您的情况下,请将5行(time1 =&time2 =&“ do until”循环)放在do循环的末尾
sub whatever()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
Do Until time1 >= time2
DoEvents
time1 = Now()
Loop
End sub
Timer
因为该Timer
方法在午夜之前失效。
kernel32.dll中的Sleep声明在64位Excel中不起作用。这会更一般一些:
#If VBA7 Then
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
只是clemo代码的清理版本-在Access中可用,而后者没有Application.Wait函数。
Public Sub Pause(sngSecs As Single)
Dim sngEnd As Single
sngEnd = Timer + sngSecs
While Timer < sngEnd
DoEvents
Wend
End Sub
Public Sub TestPause()
Pause 1
MsgBox "done"
End Sub
Timer
给出自午夜以来的秒数,则该方法在午夜之前执行(即,sngEnd
> = 86,400)失败。午夜时分,Timer
重置为0,因此比sngEnd
永远少。
所介绍的大多数解决方案都使用Application.Wait,它没有考虑自从秒数开始计数以来已经经过的时间(毫秒),因此它们的固有误差高达1秒。
计时器方法是最好的解决方案,但是您必须考虑在午夜进行重置,因此这是使用计时器的非常精确的睡眠方法:
'You can use integer (1 for 1 second) or single (1.5 for 1 and a half second)
Public Sub Sleep(vSeconds As Variant)
Dim t0 As Single, t1 As Single
t0 = Timer
Do
t1 = Timer
If t1 < t0 Then t1 = t1 + 86400 'Timer overflows at midnight
DoEvents 'optional, to avoid excel freeze while sleeping
Loop Until t1 - t0 >= vSeconds
End Sub
使用此功能测试任何睡眠功能:(打开调试即时窗口:CTRL + G)
Sub testSleep()
t0 = Timer
Debug.Print "Time before sleep:"; t0 'Timer format is in seconds since midnight
Sleep (1.5)
Debug.Print "Time after sleep:"; Timer
Debug.Print "Slept for:"; Timer - t0; "seconds"
End Sub
Function Delay(ByVal T As Integer)
'Function can be used to introduce a delay of up to 99 seconds
'Call Function ex: Delay 2 {introduces a 2 second delay before execution of code resumes}
strT = Mid((100 + T), 2, 2)
strSecsDelay = "00:00:" & strT
Application.Wait (Now + TimeValue(strSecsDelay))
End Function
这是睡眠的替代方法:
Sub TDelay(delay As Long)
Dim n As Long
For n = 1 To delay
DoEvents
Next n
End Sub
在下面的代码中,我使旋转按钮上的“发光”效果闪烁,以指示用户是否遇到“麻烦”,在循环中使用“ sleep 1000”不会导致可见的闪烁,但该循环的效果很好。
Sub SpinFocus()
Dim i As Long
For i = 1 To 3 '3 blinks
Worksheets(2).Shapes("SpinGlow").ZOrder (msoBringToFront)
TDelay (10000) 'this makes the glow stay lit longer than not, looks nice.
Worksheets(2).Shapes("SpinGlow").ZOrder (msoSendBackward)
TDelay (100)
Next i
End Sub
我这样做是为了回答问题:
Sub goTIMER(NumOfSeconds As Long) 'in (seconds) as: call gotimer (1) 'seconds
Application.Wait now + NumOfSeconds / 86400#
'Application.Wait (Now + TimeValue("0:00:05")) 'other
Application.EnableEvents = True 'EVENTS
End Sub
我通常使用Timer函数暂停应用程序。将此代码插入您的代码中
T0 = Timer
Do
Delay = Timer - T0
Loop Until Delay >= 1 'Change this value to pause time for a certain amount of seconds
Timer
给T0
出自午夜以来的秒数,则该方法在午夜之前执行(即,该时间少于自午夜起的延迟秒数)将失败。在午夜,Timer
在达到延迟限制之前重置为0。Delay
永远不会达到延迟限制,因此循环永远运行。
Loop Until Delay >= 1
,否则就有可能超过1并且永远不会退出循环。
等待和睡眠功能会锁定Excel,直到延迟结束,您才能执行其他任何操作。另一方面,循环延迟不会给您确切的等待时间。
因此,我将这种解决方法加入了两个概念。循环播放直到您需要的时间为止。
Private Sub Waste10Sec()
target = (Now + TimeValue("0:00:10"))
Do
DoEvents 'keeps excel running other stuff
Loop Until Now >= target
End Sub
您只需要在需要延迟的地方致电Waste10Sec
DoEvents
按此处的演示dailydoseofexcel.com/archives/2005/06/14/stopwatch