How to pause for specific amount of time? (Excel/VBA)
I have an Excel worksheet that has the following macro. I'd like to loop it every second but danged if I can find the function to do that. Isn't it possible?
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
'Here I want to wait for one second
Loop
End Sub
vba excel-vba excel
add a comment |
I have an Excel worksheet that has the following macro. I'd like to loop it every second but danged if I can find the function to do that. Isn't it possible?
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
'Here I want to wait for one second
Loop
End Sub
vba excel-vba excel
add a comment |
I have an Excel worksheet that has the following macro. I'd like to loop it every second but danged if I can find the function to do that. Isn't it possible?
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
'Here I want to wait for one second
Loop
End Sub
vba excel-vba excel
I have an Excel worksheet that has the following macro. I'd like to loop it every second but danged if I can find the function to do that. Isn't it possible?
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
'Here I want to wait for one second
Loop
End Sub
vba excel-vba excel
vba excel-vba excel
edited Jul 9 '18 at 19:34
Community♦
11
11
asked Oct 9 '09 at 15:34
KengKeng
31.3k2470108
31.3k2470108
add a comment |
add a comment |
13 Answers
13
active
oldest
votes
Use the Wait method:
Application.Wait Now + #0:00:01#
or (for Excel 2010 and later):
Application.Wait Now + #12:00:01 AM#
THANKS!!! is there a more graceful way of stopping it rather than using 'Esc'?
– Keng
Oct 9 '09 at 15:48
3
@Benoit and @Keng, you can put 1 second directly, avoiding to transform text to date. Is more clean for me:Application.Wait(Now + #0:00:01#)
Cheers!
– A.Sommerh
Apr 22 '14 at 18:40
27
That format wasn't working in Excel 2010. This works though :Application.Wait (Now + TimeValue("0:00:01"))
– ZX9
Mar 4 '15 at 18:56
1
DateAdd("s", 1, Now) does the right thing. And can be generalized for any long number of seconds: DateAdd("s", nSec, Now) without using the time literal. To sleep less than 1 second use the Sleep API in kernel32
– Andrew Dennison
Apr 6 '17 at 15:51
1
@ZX9 timevalue("00:00:01") = 0.0000115740... So Application.wait(now + 0.00001) is about a second. I like to useApplication.wait(now + 1e-5)
for a second,Application.wait(now + 0.5e-5)
for half a second etc.
– Some_Guy
Apr 18 '17 at 15:32
|
show 6 more comments
Add this to your module
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Or, for 64-bit systems use:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Call it in your macro like so:
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
Sleep (1000) ' delay 1 second
Loop
End Sub
1
Why not use the VBA method to do this rather than using kernel32.dll?
– Ben S
Oct 9 '09 at 15:45
9
I have used this method as it is portable between the various office products such as Access and is not Excel specific.
– Buggabill
Oct 9 '09 at 15:49
17
+1, becauseSleep()
lets you specify wait times of less than 1 second.Application.Wait
is sometimes too granular.
– BradC
May 24 '10 at 17:27
4
This does not work with 64-bit versions of office.
– Julian Knight
Jun 12 '15 at 11:50
2
@JulianKnight:Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
– Vegard
Dec 8 '16 at 7:47
|
show 4 more comments
instead of using:
Application.Wait(Now + #0:00:01#)
i prefer:
Application.Wait(Now + TimeValue("00:00:01"))
because it is a lot easier to read afterwards.
7
And it works whereas, in Office 2013, the #0:0:1# format converts to 1 second after midnight.
– Julian Knight
Jun 12 '15 at 11:51
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:54
add a comment |
this works flawlessly for me. insert any code before or after the "do until" loop. In your case, put the 5 lines (time1= & time2= & "do until" loop) at the end inside your do loop
sub whatever()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
Do Until time1 >= time2
DoEvents
time1 = Now()
Loop
End sub
1
I like this solution most because I didn't want to halt the message queue.
– Daniel Fuchs
Feb 6 '15 at 11:47
This solution is precise and does not require the declaration of the sleep-API-Function. I used to store time in double values and use microseconds instead with the same result.
– DrMarbuse
Jul 15 '15 at 17:25
This solution works better than the similar solutions below that useTimer
because theTimer
approach fails just before midnight.
– vknowles
Mar 3 '17 at 21:37
This solution is not precise, it does not take in account the milliseconds that already elapsed from current time... For example, if there is only 1 milisecond left until the Now seconds changes, you will only sleep for 1 milisecond.
– cyberponk
Nov 20 '18 at 11:19
when other solutions were taking a long time for a non-MS Office based VBA macro, this one worked perfectly - thanks!
– curious
Dec 25 '18 at 3:22
add a comment |
The declaration for Sleep in kernel32.dll won't work in 64-bit Excel. This would be a little more general:
#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
2
Fails to compile in Office 2013. The error checker in VBA for Office 2013 seems to ignore the compiler statements.
– Julian Knight
Jun 12 '15 at 11:53
2
Compiles fine for me in Office 2013.
– Jon Peltier
Jun 9 '16 at 18:16
Most people agree that Win64/VBA7 dwMilliseconds is Long, not LongPtr. Excel VBA hides errors like this by doing stack correction after external calls, but errors like this will crash most versions of Open Office
– david
Nov 9 '16 at 7:31
add a comment |
Just a cleaned up version of clemo's code - works in Access, which doesn't have the Application.Wait function.
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
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatsngEnd
is >= 86,400). At midnight,Timer
resets to 0 and thus remains less thansngEnd
forever.
– vknowles
Mar 3 '17 at 21:19
P.S. The code from @clemo above does work any time of day.
– vknowles
Mar 3 '17 at 21:36
add a comment |
Application.Wait Second(Now) + 1
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 23:33
add a comment |
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
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
Here is an alternative to sleep:
Sub TDelay(delay As Long)
Dim n As Long
For n = 1 To delay
DoEvents
Next n
End Sub
In the following code I make a "glow" effect blink on a spin button to direct users to it if they are "having trouble", using "sleep 1000" in the loop resulted in no visible blinking, but the loop is working great.
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
add a comment |
i had this made to answer the problem:
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
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
I usually use the Timer function to pause the application. Insert this code to yours
T0 = Timer
Do
Delay = Timer - T0
Loop Until Delay >= 1 'Change this value to pause time for a certain amount of seconds
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatT0
is less than the number of delay seconds from midnight). At midnight,Timer
resets to 0 before the delay limit is reached.Delay
never reaches the delay limit, so the loop runs forever.
– vknowles
Mar 3 '17 at 21:23
Since Timer produces non-integer values, you should useLoop Until Delay >= 1
, or you risk going over 1 and never exiting the loop.
– Jon Peltier
Jul 17 '18 at 21:17
add a comment |
Most of the presented solutions use Application.Wait, which does not take in account the time (miliseconds) already elapsed since the currend second count started, so they have an intrinsic imprecision of up to 1 second.
The Timer approach is the best solution, but you have to take in account the reset at midnight, so here is a very precise Sleep method using Timer:
'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
USE THIS TO TEST ANY SLEEP FUNCTION: (open debug window: 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
add a comment |
Try this :
Threading.thread.sleep(1000)
Won't work by default
– Shai Alon
Dec 24 '15 at 16:56
add a comment |
protected by Community♦ Jan 14 '17 at 11:21
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the Wait method:
Application.Wait Now + #0:00:01#
or (for Excel 2010 and later):
Application.Wait Now + #12:00:01 AM#
THANKS!!! is there a more graceful way of stopping it rather than using 'Esc'?
– Keng
Oct 9 '09 at 15:48
3
@Benoit and @Keng, you can put 1 second directly, avoiding to transform text to date. Is more clean for me:Application.Wait(Now + #0:00:01#)
Cheers!
– A.Sommerh
Apr 22 '14 at 18:40
27
That format wasn't working in Excel 2010. This works though :Application.Wait (Now + TimeValue("0:00:01"))
– ZX9
Mar 4 '15 at 18:56
1
DateAdd("s", 1, Now) does the right thing. And can be generalized for any long number of seconds: DateAdd("s", nSec, Now) without using the time literal. To sleep less than 1 second use the Sleep API in kernel32
– Andrew Dennison
Apr 6 '17 at 15:51
1
@ZX9 timevalue("00:00:01") = 0.0000115740... So Application.wait(now + 0.00001) is about a second. I like to useApplication.wait(now + 1e-5)
for a second,Application.wait(now + 0.5e-5)
for half a second etc.
– Some_Guy
Apr 18 '17 at 15:32
|
show 6 more comments
Use the Wait method:
Application.Wait Now + #0:00:01#
or (for Excel 2010 and later):
Application.Wait Now + #12:00:01 AM#
THANKS!!! is there a more graceful way of stopping it rather than using 'Esc'?
– Keng
Oct 9 '09 at 15:48
3
@Benoit and @Keng, you can put 1 second directly, avoiding to transform text to date. Is more clean for me:Application.Wait(Now + #0:00:01#)
Cheers!
– A.Sommerh
Apr 22 '14 at 18:40
27
That format wasn't working in Excel 2010. This works though :Application.Wait (Now + TimeValue("0:00:01"))
– ZX9
Mar 4 '15 at 18:56
1
DateAdd("s", 1, Now) does the right thing. And can be generalized for any long number of seconds: DateAdd("s", nSec, Now) without using the time literal. To sleep less than 1 second use the Sleep API in kernel32
– Andrew Dennison
Apr 6 '17 at 15:51
1
@ZX9 timevalue("00:00:01") = 0.0000115740... So Application.wait(now + 0.00001) is about a second. I like to useApplication.wait(now + 1e-5)
for a second,Application.wait(now + 0.5e-5)
for half a second etc.
– Some_Guy
Apr 18 '17 at 15:32
|
show 6 more comments
Use the Wait method:
Application.Wait Now + #0:00:01#
or (for Excel 2010 and later):
Application.Wait Now + #12:00:01 AM#
Use the Wait method:
Application.Wait Now + #0:00:01#
or (for Excel 2010 and later):
Application.Wait Now + #12:00:01 AM#
edited May 18 '17 at 6:51
kevinarpe
10.3k1388113
10.3k1388113
answered Oct 9 '09 at 15:40
Ben SBen S
57.1k24154205
57.1k24154205
THANKS!!! is there a more graceful way of stopping it rather than using 'Esc'?
– Keng
Oct 9 '09 at 15:48
3
@Benoit and @Keng, you can put 1 second directly, avoiding to transform text to date. Is more clean for me:Application.Wait(Now + #0:00:01#)
Cheers!
– A.Sommerh
Apr 22 '14 at 18:40
27
That format wasn't working in Excel 2010. This works though :Application.Wait (Now + TimeValue("0:00:01"))
– ZX9
Mar 4 '15 at 18:56
1
DateAdd("s", 1, Now) does the right thing. And can be generalized for any long number of seconds: DateAdd("s", nSec, Now) without using the time literal. To sleep less than 1 second use the Sleep API in kernel32
– Andrew Dennison
Apr 6 '17 at 15:51
1
@ZX9 timevalue("00:00:01") = 0.0000115740... So Application.wait(now + 0.00001) is about a second. I like to useApplication.wait(now + 1e-5)
for a second,Application.wait(now + 0.5e-5)
for half a second etc.
– Some_Guy
Apr 18 '17 at 15:32
|
show 6 more comments
THANKS!!! is there a more graceful way of stopping it rather than using 'Esc'?
– Keng
Oct 9 '09 at 15:48
3
@Benoit and @Keng, you can put 1 second directly, avoiding to transform text to date. Is more clean for me:Application.Wait(Now + #0:00:01#)
Cheers!
– A.Sommerh
Apr 22 '14 at 18:40
27
That format wasn't working in Excel 2010. This works though :Application.Wait (Now + TimeValue("0:00:01"))
– ZX9
Mar 4 '15 at 18:56
1
DateAdd("s", 1, Now) does the right thing. And can be generalized for any long number of seconds: DateAdd("s", nSec, Now) without using the time literal. To sleep less than 1 second use the Sleep API in kernel32
– Andrew Dennison
Apr 6 '17 at 15:51
1
@ZX9 timevalue("00:00:01") = 0.0000115740... So Application.wait(now + 0.00001) is about a second. I like to useApplication.wait(now + 1e-5)
for a second,Application.wait(now + 0.5e-5)
for half a second etc.
– Some_Guy
Apr 18 '17 at 15:32
THANKS!!! is there a more graceful way of stopping it rather than using 'Esc'?
– Keng
Oct 9 '09 at 15:48
THANKS!!! is there a more graceful way of stopping it rather than using 'Esc'?
– Keng
Oct 9 '09 at 15:48
3
3
@Benoit and @Keng, you can put 1 second directly, avoiding to transform text to date. Is more clean for me:
Application.Wait(Now + #0:00:01#)
Cheers!– A.Sommerh
Apr 22 '14 at 18:40
@Benoit and @Keng, you can put 1 second directly, avoiding to transform text to date. Is more clean for me:
Application.Wait(Now + #0:00:01#)
Cheers!– A.Sommerh
Apr 22 '14 at 18:40
27
27
That format wasn't working in Excel 2010. This works though :
Application.Wait (Now + TimeValue("0:00:01"))
– ZX9
Mar 4 '15 at 18:56
That format wasn't working in Excel 2010. This works though :
Application.Wait (Now + TimeValue("0:00:01"))
– ZX9
Mar 4 '15 at 18:56
1
1
DateAdd("s", 1, Now) does the right thing. And can be generalized for any long number of seconds: DateAdd("s", nSec, Now) without using the time literal. To sleep less than 1 second use the Sleep API in kernel32
– Andrew Dennison
Apr 6 '17 at 15:51
DateAdd("s", 1, Now) does the right thing. And can be generalized for any long number of seconds: DateAdd("s", nSec, Now) without using the time literal. To sleep less than 1 second use the Sleep API in kernel32
– Andrew Dennison
Apr 6 '17 at 15:51
1
1
@ZX9 timevalue("00:00:01") = 0.0000115740... So Application.wait(now + 0.00001) is about a second. I like to use
Application.wait(now + 1e-5)
for a second, Application.wait(now + 0.5e-5)
for half a second etc.– Some_Guy
Apr 18 '17 at 15:32
@ZX9 timevalue("00:00:01") = 0.0000115740... So Application.wait(now + 0.00001) is about a second. I like to use
Application.wait(now + 1e-5)
for a second, Application.wait(now + 0.5e-5)
for half a second etc.– Some_Guy
Apr 18 '17 at 15:32
|
show 6 more comments
Add this to your module
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Or, for 64-bit systems use:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Call it in your macro like so:
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
Sleep (1000) ' delay 1 second
Loop
End Sub
1
Why not use the VBA method to do this rather than using kernel32.dll?
– Ben S
Oct 9 '09 at 15:45
9
I have used this method as it is portable between the various office products such as Access and is not Excel specific.
– Buggabill
Oct 9 '09 at 15:49
17
+1, becauseSleep()
lets you specify wait times of less than 1 second.Application.Wait
is sometimes too granular.
– BradC
May 24 '10 at 17:27
4
This does not work with 64-bit versions of office.
– Julian Knight
Jun 12 '15 at 11:50
2
@JulianKnight:Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
– Vegard
Dec 8 '16 at 7:47
|
show 4 more comments
Add this to your module
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Or, for 64-bit systems use:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Call it in your macro like so:
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
Sleep (1000) ' delay 1 second
Loop
End Sub
1
Why not use the VBA method to do this rather than using kernel32.dll?
– Ben S
Oct 9 '09 at 15:45
9
I have used this method as it is portable between the various office products such as Access and is not Excel specific.
– Buggabill
Oct 9 '09 at 15:49
17
+1, becauseSleep()
lets you specify wait times of less than 1 second.Application.Wait
is sometimes too granular.
– BradC
May 24 '10 at 17:27
4
This does not work with 64-bit versions of office.
– Julian Knight
Jun 12 '15 at 11:50
2
@JulianKnight:Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
– Vegard
Dec 8 '16 at 7:47
|
show 4 more comments
Add this to your module
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Or, for 64-bit systems use:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Call it in your macro like so:
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
Sleep (1000) ' delay 1 second
Loop
End Sub
Add this to your module
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Or, for 64-bit systems use:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Call it in your macro like so:
Sub Macro1()
'
' Macro1 Macro
'
Do
Calculate
Sleep (1000) ' delay 1 second
Loop
End Sub
edited Jun 13 '17 at 19:55
LondonRob
27k1475114
27k1475114
answered Oct 9 '09 at 15:43
BuggabillBuggabill
8,51633546
8,51633546
1
Why not use the VBA method to do this rather than using kernel32.dll?
– Ben S
Oct 9 '09 at 15:45
9
I have used this method as it is portable between the various office products such as Access and is not Excel specific.
– Buggabill
Oct 9 '09 at 15:49
17
+1, becauseSleep()
lets you specify wait times of less than 1 second.Application.Wait
is sometimes too granular.
– BradC
May 24 '10 at 17:27
4
This does not work with 64-bit versions of office.
– Julian Knight
Jun 12 '15 at 11:50
2
@JulianKnight:Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
– Vegard
Dec 8 '16 at 7:47
|
show 4 more comments
1
Why not use the VBA method to do this rather than using kernel32.dll?
– Ben S
Oct 9 '09 at 15:45
9
I have used this method as it is portable between the various office products such as Access and is not Excel specific.
– Buggabill
Oct 9 '09 at 15:49
17
+1, becauseSleep()
lets you specify wait times of less than 1 second.Application.Wait
is sometimes too granular.
– BradC
May 24 '10 at 17:27
4
This does not work with 64-bit versions of office.
– Julian Knight
Jun 12 '15 at 11:50
2
@JulianKnight:Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
– Vegard
Dec 8 '16 at 7:47
1
1
Why not use the VBA method to do this rather than using kernel32.dll?
– Ben S
Oct 9 '09 at 15:45
Why not use the VBA method to do this rather than using kernel32.dll?
– Ben S
Oct 9 '09 at 15:45
9
9
I have used this method as it is portable between the various office products such as Access and is not Excel specific.
– Buggabill
Oct 9 '09 at 15:49
I have used this method as it is portable between the various office products such as Access and is not Excel specific.
– Buggabill
Oct 9 '09 at 15:49
17
17
+1, because
Sleep()
lets you specify wait times of less than 1 second. Application.Wait
is sometimes too granular.– BradC
May 24 '10 at 17:27
+1, because
Sleep()
lets you specify wait times of less than 1 second. Application.Wait
is sometimes too granular.– BradC
May 24 '10 at 17:27
4
4
This does not work with 64-bit versions of office.
– Julian Knight
Jun 12 '15 at 11:50
This does not work with 64-bit versions of office.
– Julian Knight
Jun 12 '15 at 11:50
2
2
@JulianKnight:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
– Vegard
Dec 8 '16 at 7:47
@JulianKnight:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
– Vegard
Dec 8 '16 at 7:47
|
show 4 more comments
instead of using:
Application.Wait(Now + #0:00:01#)
i prefer:
Application.Wait(Now + TimeValue("00:00:01"))
because it is a lot easier to read afterwards.
7
And it works whereas, in Office 2013, the #0:0:1# format converts to 1 second after midnight.
– Julian Knight
Jun 12 '15 at 11:51
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:54
add a comment |
instead of using:
Application.Wait(Now + #0:00:01#)
i prefer:
Application.Wait(Now + TimeValue("00:00:01"))
because it is a lot easier to read afterwards.
7
And it works whereas, in Office 2013, the #0:0:1# format converts to 1 second after midnight.
– Julian Knight
Jun 12 '15 at 11:51
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:54
add a comment |
instead of using:
Application.Wait(Now + #0:00:01#)
i prefer:
Application.Wait(Now + TimeValue("00:00:01"))
because it is a lot easier to read afterwards.
instead of using:
Application.Wait(Now + #0:00:01#)
i prefer:
Application.Wait(Now + TimeValue("00:00:01"))
because it is a lot easier to read afterwards.
answered Jun 1 '14 at 20:21
Achaibou KarimAchaibou Karim
38835
38835
7
And it works whereas, in Office 2013, the #0:0:1# format converts to 1 second after midnight.
– Julian Knight
Jun 12 '15 at 11:51
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:54
add a comment |
7
And it works whereas, in Office 2013, the #0:0:1# format converts to 1 second after midnight.
– Julian Knight
Jun 12 '15 at 11:51
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:54
7
7
And it works whereas, in Office 2013, the #0:0:1# format converts to 1 second after midnight.
– Julian Knight
Jun 12 '15 at 11:51
And it works whereas, in Office 2013, the #0:0:1# format converts to 1 second after midnight.
– Julian Knight
Jun 12 '15 at 11:51
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:54
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:54
add a comment |
this works flawlessly for me. insert any code before or after the "do until" loop. In your case, put the 5 lines (time1= & time2= & "do until" loop) at the end inside your do loop
sub whatever()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
Do Until time1 >= time2
DoEvents
time1 = Now()
Loop
End sub
1
I like this solution most because I didn't want to halt the message queue.
– Daniel Fuchs
Feb 6 '15 at 11:47
This solution is precise and does not require the declaration of the sleep-API-Function. I used to store time in double values and use microseconds instead with the same result.
– DrMarbuse
Jul 15 '15 at 17:25
This solution works better than the similar solutions below that useTimer
because theTimer
approach fails just before midnight.
– vknowles
Mar 3 '17 at 21:37
This solution is not precise, it does not take in account the milliseconds that already elapsed from current time... For example, if there is only 1 milisecond left until the Now seconds changes, you will only sleep for 1 milisecond.
– cyberponk
Nov 20 '18 at 11:19
when other solutions were taking a long time for a non-MS Office based VBA macro, this one worked perfectly - thanks!
– curious
Dec 25 '18 at 3:22
add a comment |
this works flawlessly for me. insert any code before or after the "do until" loop. In your case, put the 5 lines (time1= & time2= & "do until" loop) at the end inside your do loop
sub whatever()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
Do Until time1 >= time2
DoEvents
time1 = Now()
Loop
End sub
1
I like this solution most because I didn't want to halt the message queue.
– Daniel Fuchs
Feb 6 '15 at 11:47
This solution is precise and does not require the declaration of the sleep-API-Function. I used to store time in double values and use microseconds instead with the same result.
– DrMarbuse
Jul 15 '15 at 17:25
This solution works better than the similar solutions below that useTimer
because theTimer
approach fails just before midnight.
– vknowles
Mar 3 '17 at 21:37
This solution is not precise, it does not take in account the milliseconds that already elapsed from current time... For example, if there is only 1 milisecond left until the Now seconds changes, you will only sleep for 1 milisecond.
– cyberponk
Nov 20 '18 at 11:19
when other solutions were taking a long time for a non-MS Office based VBA macro, this one worked perfectly - thanks!
– curious
Dec 25 '18 at 3:22
add a comment |
this works flawlessly for me. insert any code before or after the "do until" loop. In your case, put the 5 lines (time1= & time2= & "do until" loop) at the end inside your do loop
sub whatever()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
Do Until time1 >= time2
DoEvents
time1 = Now()
Loop
End sub
this works flawlessly for me. insert any code before or after the "do until" loop. In your case, put the 5 lines (time1= & time2= & "do until" loop) at the end inside your do loop
sub whatever()
Dim time1, time2
time1 = Now
time2 = Now + TimeValue("0:00:01")
Do Until time1 >= time2
DoEvents
time1 = Now()
Loop
End sub
answered Oct 26 '13 at 2:38
clemoclemo
17912
17912
1
I like this solution most because I didn't want to halt the message queue.
– Daniel Fuchs
Feb 6 '15 at 11:47
This solution is precise and does not require the declaration of the sleep-API-Function. I used to store time in double values and use microseconds instead with the same result.
– DrMarbuse
Jul 15 '15 at 17:25
This solution works better than the similar solutions below that useTimer
because theTimer
approach fails just before midnight.
– vknowles
Mar 3 '17 at 21:37
This solution is not precise, it does not take in account the milliseconds that already elapsed from current time... For example, if there is only 1 milisecond left until the Now seconds changes, you will only sleep for 1 milisecond.
– cyberponk
Nov 20 '18 at 11:19
when other solutions were taking a long time for a non-MS Office based VBA macro, this one worked perfectly - thanks!
– curious
Dec 25 '18 at 3:22
add a comment |
1
I like this solution most because I didn't want to halt the message queue.
– Daniel Fuchs
Feb 6 '15 at 11:47
This solution is precise and does not require the declaration of the sleep-API-Function. I used to store time in double values and use microseconds instead with the same result.
– DrMarbuse
Jul 15 '15 at 17:25
This solution works better than the similar solutions below that useTimer
because theTimer
approach fails just before midnight.
– vknowles
Mar 3 '17 at 21:37
This solution is not precise, it does not take in account the milliseconds that already elapsed from current time... For example, if there is only 1 milisecond left until the Now seconds changes, you will only sleep for 1 milisecond.
– cyberponk
Nov 20 '18 at 11:19
when other solutions were taking a long time for a non-MS Office based VBA macro, this one worked perfectly - thanks!
– curious
Dec 25 '18 at 3:22
1
1
I like this solution most because I didn't want to halt the message queue.
– Daniel Fuchs
Feb 6 '15 at 11:47
I like this solution most because I didn't want to halt the message queue.
– Daniel Fuchs
Feb 6 '15 at 11:47
This solution is precise and does not require the declaration of the sleep-API-Function. I used to store time in double values and use microseconds instead with the same result.
– DrMarbuse
Jul 15 '15 at 17:25
This solution is precise and does not require the declaration of the sleep-API-Function. I used to store time in double values and use microseconds instead with the same result.
– DrMarbuse
Jul 15 '15 at 17:25
This solution works better than the similar solutions below that use
Timer
because the Timer
approach fails just before midnight.– vknowles
Mar 3 '17 at 21:37
This solution works better than the similar solutions below that use
Timer
because the Timer
approach fails just before midnight.– vknowles
Mar 3 '17 at 21:37
This solution is not precise, it does not take in account the milliseconds that already elapsed from current time... For example, if there is only 1 milisecond left until the Now seconds changes, you will only sleep for 1 milisecond.
– cyberponk
Nov 20 '18 at 11:19
This solution is not precise, it does not take in account the milliseconds that already elapsed from current time... For example, if there is only 1 milisecond left until the Now seconds changes, you will only sleep for 1 milisecond.
– cyberponk
Nov 20 '18 at 11:19
when other solutions were taking a long time for a non-MS Office based VBA macro, this one worked perfectly - thanks!
– curious
Dec 25 '18 at 3:22
when other solutions were taking a long time for a non-MS Office based VBA macro, this one worked perfectly - thanks!
– curious
Dec 25 '18 at 3:22
add a comment |
The declaration for Sleep in kernel32.dll won't work in 64-bit Excel. This would be a little more general:
#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
2
Fails to compile in Office 2013. The error checker in VBA for Office 2013 seems to ignore the compiler statements.
– Julian Knight
Jun 12 '15 at 11:53
2
Compiles fine for me in Office 2013.
– Jon Peltier
Jun 9 '16 at 18:16
Most people agree that Win64/VBA7 dwMilliseconds is Long, not LongPtr. Excel VBA hides errors like this by doing stack correction after external calls, but errors like this will crash most versions of Open Office
– david
Nov 9 '16 at 7:31
add a comment |
The declaration for Sleep in kernel32.dll won't work in 64-bit Excel. This would be a little more general:
#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
2
Fails to compile in Office 2013. The error checker in VBA for Office 2013 seems to ignore the compiler statements.
– Julian Knight
Jun 12 '15 at 11:53
2
Compiles fine for me in Office 2013.
– Jon Peltier
Jun 9 '16 at 18:16
Most people agree that Win64/VBA7 dwMilliseconds is Long, not LongPtr. Excel VBA hides errors like this by doing stack correction after external calls, but errors like this will crash most versions of Open Office
– david
Nov 9 '16 at 7:31
add a comment |
The declaration for Sleep in kernel32.dll won't work in 64-bit Excel. This would be a little more general:
#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
The declaration for Sleep in kernel32.dll won't work in 64-bit Excel. This would be a little more general:
#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
edited Dec 18 '17 at 1:51
Gilles Gouaillardet
4,44551323
4,44551323
answered May 8 '14 at 19:31
dbuskirkdbuskirk
31137
31137
2
Fails to compile in Office 2013. The error checker in VBA for Office 2013 seems to ignore the compiler statements.
– Julian Knight
Jun 12 '15 at 11:53
2
Compiles fine for me in Office 2013.
– Jon Peltier
Jun 9 '16 at 18:16
Most people agree that Win64/VBA7 dwMilliseconds is Long, not LongPtr. Excel VBA hides errors like this by doing stack correction after external calls, but errors like this will crash most versions of Open Office
– david
Nov 9 '16 at 7:31
add a comment |
2
Fails to compile in Office 2013. The error checker in VBA for Office 2013 seems to ignore the compiler statements.
– Julian Knight
Jun 12 '15 at 11:53
2
Compiles fine for me in Office 2013.
– Jon Peltier
Jun 9 '16 at 18:16
Most people agree that Win64/VBA7 dwMilliseconds is Long, not LongPtr. Excel VBA hides errors like this by doing stack correction after external calls, but errors like this will crash most versions of Open Office
– david
Nov 9 '16 at 7:31
2
2
Fails to compile in Office 2013. The error checker in VBA for Office 2013 seems to ignore the compiler statements.
– Julian Knight
Jun 12 '15 at 11:53
Fails to compile in Office 2013. The error checker in VBA for Office 2013 seems to ignore the compiler statements.
– Julian Knight
Jun 12 '15 at 11:53
2
2
Compiles fine for me in Office 2013.
– Jon Peltier
Jun 9 '16 at 18:16
Compiles fine for me in Office 2013.
– Jon Peltier
Jun 9 '16 at 18:16
Most people agree that Win64/VBA7 dwMilliseconds is Long, not LongPtr. Excel VBA hides errors like this by doing stack correction after external calls, but errors like this will crash most versions of Open Office
– david
Nov 9 '16 at 7:31
Most people agree that Win64/VBA7 dwMilliseconds is Long, not LongPtr. Excel VBA hides errors like this by doing stack correction after external calls, but errors like this will crash most versions of Open Office
– david
Nov 9 '16 at 7:31
add a comment |
Just a cleaned up version of clemo's code - works in Access, which doesn't have the Application.Wait function.
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
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatsngEnd
is >= 86,400). At midnight,Timer
resets to 0 and thus remains less thansngEnd
forever.
– vknowles
Mar 3 '17 at 21:19
P.S. The code from @clemo above does work any time of day.
– vknowles
Mar 3 '17 at 21:36
add a comment |
Just a cleaned up version of clemo's code - works in Access, which doesn't have the Application.Wait function.
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
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatsngEnd
is >= 86,400). At midnight,Timer
resets to 0 and thus remains less thansngEnd
forever.
– vknowles
Mar 3 '17 at 21:19
P.S. The code from @clemo above does work any time of day.
– vknowles
Mar 3 '17 at 21:36
add a comment |
Just a cleaned up version of clemo's code - works in Access, which doesn't have the Application.Wait function.
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
Just a cleaned up version of clemo's code - works in Access, which doesn't have the Application.Wait function.
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
answered Sep 18 '15 at 6:20
Brian BurnsBrian Burns
6,70254545
6,70254545
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatsngEnd
is >= 86,400). At midnight,Timer
resets to 0 and thus remains less thansngEnd
forever.
– vknowles
Mar 3 '17 at 21:19
P.S. The code from @clemo above does work any time of day.
– vknowles
Mar 3 '17 at 21:36
add a comment |
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatsngEnd
is >= 86,400). At midnight,Timer
resets to 0 and thus remains less thansngEnd
forever.
– vknowles
Mar 3 '17 at 21:19
P.S. The code from @clemo above does work any time of day.
– vknowles
Mar 3 '17 at 21:36
1
1
If
Timer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such that sngEnd
is >= 86,400). At midnight, Timer
resets to 0 and thus remains less than sngEnd
forever.– vknowles
Mar 3 '17 at 21:19
If
Timer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such that sngEnd
is >= 86,400). At midnight, Timer
resets to 0 and thus remains less than sngEnd
forever.– vknowles
Mar 3 '17 at 21:19
P.S. The code from @clemo above does work any time of day.
– vknowles
Mar 3 '17 at 21:36
P.S. The code from @clemo above does work any time of day.
– vknowles
Mar 3 '17 at 21:36
add a comment |
Application.Wait Second(Now) + 1
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 23:33
add a comment |
Application.Wait Second(Now) + 1
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 23:33
add a comment |
Application.Wait Second(Now) + 1
Application.Wait Second(Now) + 1
edited Jul 13 '14 at 21:13
Nathaniel Ford
13.7k145473
13.7k145473
answered Jul 13 '14 at 20:04
g tg t
391
391
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 23:33
add a comment |
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 23:33
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 23:33
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 23:33
add a comment |
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
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
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
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
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
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
edited Dec 17 '14 at 19:44
Stanley
1,04451732
1,04451732
answered Dec 17 '14 at 18:45
ITIITI
211
211
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
Here is an alternative to sleep:
Sub TDelay(delay As Long)
Dim n As Long
For n = 1 To delay
DoEvents
Next n
End Sub
In the following code I make a "glow" effect blink on a spin button to direct users to it if they are "having trouble", using "sleep 1000" in the loop resulted in no visible blinking, but the loop is working great.
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
add a comment |
Here is an alternative to sleep:
Sub TDelay(delay As Long)
Dim n As Long
For n = 1 To delay
DoEvents
Next n
End Sub
In the following code I make a "glow" effect blink on a spin button to direct users to it if they are "having trouble", using "sleep 1000" in the loop resulted in no visible blinking, but the loop is working great.
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
add a comment |
Here is an alternative to sleep:
Sub TDelay(delay As Long)
Dim n As Long
For n = 1 To delay
DoEvents
Next n
End Sub
In the following code I make a "glow" effect blink on a spin button to direct users to it if they are "having trouble", using "sleep 1000" in the loop resulted in no visible blinking, but the loop is working great.
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
Here is an alternative to sleep:
Sub TDelay(delay As Long)
Dim n As Long
For n = 1 To delay
DoEvents
Next n
End Sub
In the following code I make a "glow" effect blink on a spin button to direct users to it if they are "having trouble", using "sleep 1000" in the loop resulted in no visible blinking, but the loop is working great.
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
answered Jan 11 '18 at 4:20
ReverusReverus
938
938
add a comment |
add a comment |
i had this made to answer the problem:
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
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
i had this made to answer the problem:
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
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
i had this made to answer the problem:
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
i had this made to answer the problem:
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
edited Dec 13 '14 at 12:42
answered Dec 13 '14 at 12:35
davedave
112
112
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
WARNING: All solutions using 'Application.Wait' have 1 second imprecision. This method does not consider the milliseconds that already elapsed since the current second started... For example, if there is only 1 milisecond left until the time seconds change, you will only sleep for 1 milisecond. You are just comparing 2 rounded numbers instead of waiting for 1 second!
– cyberponk
Nov 21 '18 at 17:58
add a comment |
I usually use the Timer function to pause the application. Insert this code to yours
T0 = Timer
Do
Delay = Timer - T0
Loop Until Delay >= 1 'Change this value to pause time for a certain amount of seconds
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatT0
is less than the number of delay seconds from midnight). At midnight,Timer
resets to 0 before the delay limit is reached.Delay
never reaches the delay limit, so the loop runs forever.
– vknowles
Mar 3 '17 at 21:23
Since Timer produces non-integer values, you should useLoop Until Delay >= 1
, or you risk going over 1 and never exiting the loop.
– Jon Peltier
Jul 17 '18 at 21:17
add a comment |
I usually use the Timer function to pause the application. Insert this code to yours
T0 = Timer
Do
Delay = Timer - T0
Loop Until Delay >= 1 'Change this value to pause time for a certain amount of seconds
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatT0
is less than the number of delay seconds from midnight). At midnight,Timer
resets to 0 before the delay limit is reached.Delay
never reaches the delay limit, so the loop runs forever.
– vknowles
Mar 3 '17 at 21:23
Since Timer produces non-integer values, you should useLoop Until Delay >= 1
, or you risk going over 1 and never exiting the loop.
– Jon Peltier
Jul 17 '18 at 21:17
add a comment |
I usually use the Timer function to pause the application. Insert this code to yours
T0 = Timer
Do
Delay = Timer - T0
Loop Until Delay >= 1 'Change this value to pause time for a certain amount of seconds
I usually use the Timer function to pause the application. Insert this code to yours
T0 = Timer
Do
Delay = Timer - T0
Loop Until Delay >= 1 'Change this value to pause time for a certain amount of seconds
edited Jul 19 '18 at 8:49
answered Jul 2 '16 at 23:32
Anastasiya-Romanova 秀Anastasiya-Romanova 秀
1,9341231
1,9341231
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatT0
is less than the number of delay seconds from midnight). At midnight,Timer
resets to 0 before the delay limit is reached.Delay
never reaches the delay limit, so the loop runs forever.
– vknowles
Mar 3 '17 at 21:23
Since Timer produces non-integer values, you should useLoop Until Delay >= 1
, or you risk going over 1 and never exiting the loop.
– Jon Peltier
Jul 17 '18 at 21:17
add a comment |
1
IfTimer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such thatT0
is less than the number of delay seconds from midnight). At midnight,Timer
resets to 0 before the delay limit is reached.Delay
never reaches the delay limit, so the loop runs forever.
– vknowles
Mar 3 '17 at 21:23
Since Timer produces non-integer values, you should useLoop Until Delay >= 1
, or you risk going over 1 and never exiting the loop.
– Jon Peltier
Jul 17 '18 at 21:17
1
1
If
Timer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such that T0
is less than the number of delay seconds from midnight). At midnight, Timer
resets to 0 before the delay limit is reached. Delay
never reaches the delay limit, so the loop runs forever.– vknowles
Mar 3 '17 at 21:23
If
Timer
gives the number of seconds since midnight, then this approach fails if it is executed just before midnight (i.e., such that T0
is less than the number of delay seconds from midnight). At midnight, Timer
resets to 0 before the delay limit is reached. Delay
never reaches the delay limit, so the loop runs forever.– vknowles
Mar 3 '17 at 21:23
Since Timer produces non-integer values, you should use
Loop Until Delay >= 1
, or you risk going over 1 and never exiting the loop.– Jon Peltier
Jul 17 '18 at 21:17
Since Timer produces non-integer values, you should use
Loop Until Delay >= 1
, or you risk going over 1 and never exiting the loop.– Jon Peltier
Jul 17 '18 at 21:17
add a comment |
Most of the presented solutions use Application.Wait, which does not take in account the time (miliseconds) already elapsed since the currend second count started, so they have an intrinsic imprecision of up to 1 second.
The Timer approach is the best solution, but you have to take in account the reset at midnight, so here is a very precise Sleep method using Timer:
'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
USE THIS TO TEST ANY SLEEP FUNCTION: (open debug window: 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
add a comment |
Most of the presented solutions use Application.Wait, which does not take in account the time (miliseconds) already elapsed since the currend second count started, so they have an intrinsic imprecision of up to 1 second.
The Timer approach is the best solution, but you have to take in account the reset at midnight, so here is a very precise Sleep method using Timer:
'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
USE THIS TO TEST ANY SLEEP FUNCTION: (open debug window: 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
add a comment |
Most of the presented solutions use Application.Wait, which does not take in account the time (miliseconds) already elapsed since the currend second count started, so they have an intrinsic imprecision of up to 1 second.
The Timer approach is the best solution, but you have to take in account the reset at midnight, so here is a very precise Sleep method using Timer:
'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
USE THIS TO TEST ANY SLEEP FUNCTION: (open debug window: 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
Most of the presented solutions use Application.Wait, which does not take in account the time (miliseconds) already elapsed since the currend second count started, so they have an intrinsic imprecision of up to 1 second.
The Timer approach is the best solution, but you have to take in account the reset at midnight, so here is a very precise Sleep method using Timer:
'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
USE THIS TO TEST ANY SLEEP FUNCTION: (open debug window: 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
edited Dec 13 '18 at 11:50
answered Nov 20 '18 at 11:52
cyberponkcyberponk
655614
655614
add a comment |
add a comment |
Try this :
Threading.thread.sleep(1000)
Won't work by default
– Shai Alon
Dec 24 '15 at 16:56
add a comment |
Try this :
Threading.thread.sleep(1000)
Won't work by default
– Shai Alon
Dec 24 '15 at 16:56
add a comment |
Try this :
Threading.thread.sleep(1000)
Try this :
Threading.thread.sleep(1000)
answered Aug 26 '13 at 13:17
DEVDEV
331
331
Won't work by default
– Shai Alon
Dec 24 '15 at 16:56
add a comment |
Won't work by default
– Shai Alon
Dec 24 '15 at 16:56
Won't work by default
– Shai Alon
Dec 24 '15 at 16:56
Won't work by default
– Shai Alon
Dec 24 '15 at 16:56
add a comment |
protected by Community♦ Jan 14 '17 at 11:21
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?