How to pause for specific amount of time? (Excel/VBA)












93















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









share|improve this question





























    93















    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









    share|improve this question



























      93












      93








      93


      21






      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









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 9 '18 at 19:34









      Community

      11




      11










      asked Oct 9 '09 at 15:34









      KengKeng

      31.3k2470108




      31.3k2470108
























          13 Answers
          13






          active

          oldest

          votes


















          114














          Use the Wait method:



          Application.Wait Now + #0:00:01#


          or (for Excel 2010 and later):



          Application.Wait Now + #12:00:01 AM#





          share|improve this answer


























          • 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 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



















          58














          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





          share|improve this answer





















          • 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, 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





            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



















          38














          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.






          share|improve this answer



















          • 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



















          17














          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





          share|improve this answer



















          • 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 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













          • 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



















          12














          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





          share|improve this answer





















          • 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



















          5














          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





          share|improve this answer



















          • 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











          • P.S. The code from @clemo above does work any time of day.

            – vknowles
            Mar 3 '17 at 21:36



















          3














          Application.Wait Second(Now) + 1






          share|improve this answer


























          • 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



















          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





          share|improve this answer


























          • 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



















          2














          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





          share|improve this answer































            1














            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





            share|improve this answer


























            • 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



















            1














            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





            share|improve this answer





















            • 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











            • 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





















            1














            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





            share|improve this answer

































              0














              Try this :



              Threading.thread.sleep(1000)





              share|improve this answer
























              • Won't work by default

                – Shai Alon
                Dec 24 '15 at 16:56










              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









              114














              Use the Wait method:



              Application.Wait Now + #0:00:01#


              or (for Excel 2010 and later):



              Application.Wait Now + #12:00:01 AM#





              share|improve this answer


























              • 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 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
















              114














              Use the Wait method:



              Application.Wait Now + #0:00:01#


              or (for Excel 2010 and later):



              Application.Wait Now + #12:00:01 AM#





              share|improve this answer


























              • 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 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














              114












              114








              114







              Use the Wait method:



              Application.Wait Now + #0:00:01#


              or (for Excel 2010 and later):



              Application.Wait Now + #12:00:01 AM#





              share|improve this answer















              Use the Wait method:



              Application.Wait Now + #0:00:01#


              or (for Excel 2010 and later):



              Application.Wait Now + #12:00:01 AM#






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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 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



















              • 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 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

















              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













              58














              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





              share|improve this answer





















              • 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, 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





                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
















              58














              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





              share|improve this answer





















              • 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, 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





                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














              58












              58








              58







              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





              share|improve this answer















              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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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, 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





                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





                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, 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





                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











              38














              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.






              share|improve this answer



















              • 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
















              38














              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.






              share|improve this answer



















              • 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














              38












              38








              38







              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.






              share|improve this answer













              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              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














              • 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











              17














              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





              share|improve this answer



















              • 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 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













              • 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
















              17














              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





              share|improve this answer



















              • 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 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













              • 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














              17












              17








              17







              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





              share|improve this answer













              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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              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 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













              • 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





                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 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













              • 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











              12














              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





              share|improve this answer





















              • 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
















              12














              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





              share|improve this answer





















              • 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














              12












              12








              12







              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





              share|improve this answer















              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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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














              • 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











              5














              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





              share|improve this answer



















              • 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











              • P.S. The code from @clemo above does work any time of day.

                – vknowles
                Mar 3 '17 at 21:36
















              5














              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





              share|improve this answer



















              • 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











              • P.S. The code from @clemo above does work any time of day.

                – vknowles
                Mar 3 '17 at 21:36














              5












              5








              5







              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





              share|improve this answer













              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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 18 '15 at 6:20









              Brian BurnsBrian Burns

              6,70254545




              6,70254545








              • 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











              • P.S. The code from @clemo above does work any time of day.

                – vknowles
                Mar 3 '17 at 21:36














              • 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











              • 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











              3














              Application.Wait Second(Now) + 1






              share|improve this answer


























              • 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
















              3














              Application.Wait Second(Now) + 1






              share|improve this answer


























              • 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














              3












              3








              3







              Application.Wait Second(Now) + 1






              share|improve this answer















              Application.Wait Second(Now) + 1







              share|improve this answer














              share|improve this answer



              share|improve this answer








              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



















              • 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











              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





              share|improve this answer


























              • 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
















              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





              share|improve this answer


























              • 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














              2












              2








              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





              share|improve this answer















              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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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



















              • 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











              2














              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





              share|improve this answer




























                2














                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





                share|improve this answer


























                  2












                  2








                  2







                  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





                  share|improve this answer













                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 11 '18 at 4:20









                  ReverusReverus

                  938




                  938























                      1














                      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





                      share|improve this answer


























                      • 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
















                      1














                      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





                      share|improve this answer


























                      • 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














                      1












                      1








                      1







                      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





                      share|improve this answer















                      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






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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



















                      • 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











                      1














                      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





                      share|improve this answer





















                      • 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











                      • 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


















                      1














                      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





                      share|improve this answer





















                      • 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











                      • 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
















                      1












                      1








                      1







                      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





                      share|improve this answer















                      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






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 19 '18 at 8:49

























                      answered Jul 2 '16 at 23:32









                      Anastasiya-Romanova 秀Anastasiya-Romanova 秀

                      1,9341231




                      1,9341231








                      • 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











                      • 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
















                      • 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











                      • 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










                      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













                      1














                      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





                      share|improve this answer






























                        1














                        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





                        share|improve this answer




























                          1












                          1








                          1







                          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





                          share|improve this answer















                          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






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 13 '18 at 11:50

























                          answered Nov 20 '18 at 11:52









                          cyberponkcyberponk

                          655614




                          655614























                              0














                              Try this :



                              Threading.thread.sleep(1000)





                              share|improve this answer
























                              • Won't work by default

                                – Shai Alon
                                Dec 24 '15 at 16:56
















                              0














                              Try this :



                              Threading.thread.sleep(1000)





                              share|improve this answer
























                              • Won't work by default

                                – Shai Alon
                                Dec 24 '15 at 16:56














                              0












                              0








                              0







                              Try this :



                              Threading.thread.sleep(1000)





                              share|improve this answer













                              Try this :



                              Threading.thread.sleep(1000)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 26 '13 at 13:17









                              DEVDEV

                              331




                              331













                              • 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





                              Won't work by default

                              – Shai Alon
                              Dec 24 '15 at 16:56





                              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?



                              Popular posts from this blog

                              Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                              ComboBox Display Member on multiple fields

                              Is it possible to collect Nectar points via Trainline?