VBA pausing by tenths of seconds (or milliseconds)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ms9
    New Member
    • Nov 2007
    • 3

    VBA pausing by tenths of seconds (or milliseconds)

    I want to pause for a value < of 1 sec

    this is my routine for 1 sec
    Sub Macro1()
    Range("C15").Se lect
    ActiveCell.Form ulaR1C1 = "12"
    Range("E26").Se lect
    Application.Wai t (Now + TimeValue("0:00 :01"))
    End Sub


    I tried with "0:00:00:10 " arguments (to set the pause to 1/10 seconds)
    Application.Wai t (Now + TimeValue("0:00 :00:10"))
    this is not working !

    any idea ?
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    Try this for (1/10th of second) :

    [code=vb]
    Application.Wai t (Now + 0.000001)
    [/code]

    Regards
    Veena

    Comment

    • ms9
      New Member
      • Nov 2007
      • 3

      #3
      Back to the 1/10e sec issue, I did replace the line

      Application.Wai t (Now + ("00:00:01") ) - works fine for 1sec
      by
      Application.Wai t (Now + 0.00001) - works fine for 1sec

      then I tried
      Application.Wai t (Now + 0.000001) - 1/10sec the macro does execute (no bugs) but no action at all 1/10sec perhaps to fast

      then I tried any other digits eg.
      Application.Wai t (Now + 0.000005) - 5/10sec the macro does not execute



      Range("C15").Se lect
      ActiveCell.Form ulaR1C1 = "12"
      Range("E26").Se lect
      Application.Wai t (Now + 0.00001)

      So still the same problem no way to get shorter than 1 sec ! (although your syntax works fine for 1sec)


      Originally posted by QVeen72
      Hi,

      Try this for (1/10th of second) :

      [code=vb]
      Application.Wai t (Now + 0.000001)
      [/code]

      Regards
      Veena

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        May it is too quick to notice any kind of difference....
        But that is how Milliseconds are passed for Wait..


        Regards
        Veena

        Comment

        • ms9
          New Member
          • Nov 2007
          • 3

          #5
          Trust you but why :
          -the only digit working is 1 ?
          -The routine is used to make a shape moving in excel whith a pause, 1 sec is working (the shape does move) and 1/10sec have no action at all on the shape ?
          Regards


          Originally posted by QVeen72
          Hi,

          May it is too quick to notice any kind of difference....
          But that is how Milliseconds are passed for Wait..


          Regards
          Veena

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            I think we have established in prior threads that Wait and OnTime functions only have a "resolution " of one second.

            For finer control, I think you'll need to use API calls.

            Comment

            • tonyk

              #7
              run a time wasting loop

              quick fix .

              write a for loop that does pretty nothing but takes a few milliseconds to run. e.g
              for i = 1 to 100,000
              blah blah blah do some stuff
              next for

              .. then get back to your graphics code. the loop should kill a milisecond or so.
              hide whatever ranges the loop stuff is acting on

              Comment

              • Tage
                New Member
                • Nov 2014
                • 1

                #8
                Code:
                Option Explicit
                Private Declare Function timeGetTime Lib "winmm.dll" () As Long
                Public lngStartTime As Long   'time in msec
                Public Sub StartTimer()
                    lngStartTime = timeGetTime()
                End Sub
                Public Function EndTimer() As Double
                    EndTimer = timeGetTime() - lngStartTime
                End Function
                Public Sub delay(msdelay As Long)
                   ' creates delay in ms
                   Dim temp As Double
                   StartTimer
                   Do Until EndTimer > (msdelay)
                   Loop
                End Sub

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Nice.

                  But shouldn't lngStartTime be a Double?

                  Comment

                  • twinnyfo
                    Recognized Expert Moderator Specialist
                    • Nov 2011
                    • 3653

                    #10
                    How about something as simple as this:

                    Code:
                    Public Function PauseEvent(Delay As Double)
                        Dim dblTimer As Double
                        dblTimer = Timer
                        Do While Timer - dblTimer < Delay
                        Loop
                    End Function
                    It's a function you can call from anywhere, just include the pause (in seconds) that you want the application to wait.

                    Usage:

                    Code:
                    PauseEvent(2.5)
                    Will cause whatever you are doing to wait for 2.5 seconds. It may not be 100% perfect regarding time, but the timer call takes time in and of itself.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Looks good.

                      I do have a couple of small suggestions, though.

                      First, put a DoEvents inside that loop to allow Windows to do other things.

                      Also, while the impact would be tiny in this case, as a general rule I recommend avoiding unnecessary calculations. In other words, rather than doing a calculation every time around the loop to determine the elapsed time, I'd just calculate the ending time once, and check that. Something long these lines...
                      Code:
                      Public Function PauseEvent(ByVal Delay As Double)
                        Dim dblEndTime As Double
                        dblEndTime = Timer + Delay
                        Do While Timer < dblEndTime
                          DoEvents
                        Loop
                      End Function

                      Comment

                      • twinnyfo
                        Recognized Expert Moderator Specialist
                        • Nov 2011
                        • 3653

                        #12
                        Great suggestions, Killer! I put about three minutes thought into my Function. Now that I've put five in, it makes much better sense!

                        Thanks!

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Yeah, I find that if you make a habit of putting in that little extra effort up front to keep your code tight, you often save time fussing with it later.

                          A bit like the old "measure twice, cut once" rule in building. :-)

                          Comment

                          • xklownx
                            New Member
                            • May 2015
                            • 1

                            #14
                            Thank you killer42 for the great suggestion. Works like a charm.

                            Comment

                            • trystan4861
                              New Member
                              • Sep 2015
                              • 3

                              #15
                              Code:
                              Public Declare Sub Delay Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
                              
                              'to use it where you want to wait... delay X 
                              'where X is the amount of milliseconds to wait for
                              'only one thing, while the program is waiting... nothing will be executed until the time selected to delay
                              delay 50
                              Last edited by Rabbit; Sep 17 '15, 03:08 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

                              Comment

                              Working...