• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1045
  • Last Modified:

Implementing Sleep Function from VBA

I'm trying to implement the Sleep function from within a VBA module.  It compiles without error, but at run-time, the execution hangs the first time the subroutine gets called.

Here's my code:

Private Declare Function vSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Const DelayTime = 20

Sub CommSleep(WaitMilliSec As Long)
    If WaitMilliSec > 0 Then
        Call vSleep(WaitMilliSec)
    End If
End Sub

... and to call the sleep function...

...
    Call CommSleep(DelayTime)
...


Any ideas what I'm doing wrong?

0
Mckenzma
Asked:
Mckenzma
  • 2
1 Solution
 
Dave BrettCommented:
Call it as a Sub, ie

Private Declare Sub vSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Const DelayTime = 20

Sub CommSleep(WaitMilliSec As Long)
    If WaitMilliSec > 0 Then vSleep (WaitMilliSec)
End Sub

Sub start()
Call CommSleep(DelayTime)
End Sub
   
Cheers

dave
0
 
wraith821Commented:
This does it in seconds. If using VBScript the Doevents will need to be commented out. In VBA(used in MS Office VBA) i think it will work.


Const DelayTime = 20

Call CommSleep DelayTime

Sub CommSleep(WaitInSeconds)
          Dim Starter
      Dim Finisher
      Dim Eta
      Starter = Timer
      Do until Eta >= WaitInSec
            DoEvents            
            Finisher = Timer
            Eta = Finisher - Starter
      loop
      msgbox "Time is Up"
End Sub
0
 
MckenzmaAuthor Commented:
Dave, you're The Man.  Thanks!
0
 
Dave BrettCommented:
Thanks for the grade
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now