我有一个包含以下宏的Excel工作表。我想每秒循环一次,但如果我能找到这样做的功能,那就很危险。不可能吗?
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
'Here I want to wait for one second
Loop
End Sub
答案 0 :(得分:118)
使用Wait method:
Application.Wait Now + #0:00:01#
或(对于Excel 2010及更高版本):
Application.Wait Now + #12:00:01 AM#
答案 1 :(得分:58)
将此添加到您的模块
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
答案 2 :(得分:38)
而不是使用:
Application.Wait(Now + #0:00:01#)
我更喜欢:
Application.Wait(Now + TimeValue("00:00:01"))
因为之后阅读起来容易得多。
答案 3 :(得分:17)
这对我来说完美无瑕。在“do until”循环之前或之后插入任何代码。在你的情况下,在你的do循环中放入5行(time1 =& time2 =&“do until”循环)
sub whatever()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
Do Until time1 >= time2
DoEvents
time1 = Now()
Loop
End sub
答案 4 :(得分:12)
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
答案 5 :(得分:5)
只是一个清理过的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
答案 6 :(得分:3)
Application.Wait Second(Now) + 1
答案 7 :(得分:2)
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
答案 8 :(得分:2)
这是睡眠的替代方案:
Sub TDelay(delay As Long)
Dim n As Long
For n = 1 To delay
DoEvents
Next n
End Sub
在下面的代码中,我做了一个&#34;发光&#34;效果会在旋转按钮上闪烁,以便在用户遇到麻烦时使用#34;使用&#34;睡眠1000&#34;在循环中导致没有可见的闪烁,但循环工作得很好。
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
答案 9 :(得分:2)
大多数提出的解决方案都使用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
答案 10 :(得分:1)
我这样做是为了回答这个问题:
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
答案 11 :(得分:1)
我通常使用计时器功能来暂停应用程序。将此代码插入您的
T0 = Timer
Do
Delay = Timer - T0
Loop Until Delay >= 1 'Change this value to pause time for a certain amount of seconds
答案 12 :(得分: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
答案 13 :(得分:0)
试试这个:
Threading.thread.sleep(1000)
答案 14 :(得分:0)
您可以使用 Application.wait now + timevalue(“ 00:00:01”) 要么 Application.wait now + timeserial(0,0,1)