VBA Sleep Doesn't Work

sooprise picture sooprise · Apr 29, 2010 · Viewed 51.7k times · Source

I know I'm doing something wrong here. I'm trying to use the sleep function to delay my code, but I get "Sub or Function not defined" error. Any tips?

Answer

SLaks picture SLaks · Apr 29, 2010

VBA does not have a Sleep function.

You can import it from Kernel32.dll like this:

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Note that this will freeze the application.
You can also call DoEvents in a While loop, which won't freeze the application.