• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1059
  • 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
 
DaveCommented:
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
 
DaveCommented:
Thanks for the grade
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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