Put your macro to Sleep
Problem
Sometimes you need to make a macro wait for something else to happen.
If you just need to kill time, you can add a loop, but while the loop runs, it eats all available processor cycles. Nothing else can run during the loop.
If you're waiting for some other process on the computer to finish, your loop will prevent the other process from executing, so it'll never finish. You'll be looping for a long, long, time.
You can include a DoEvents statement within the loop to force your code to yield processor cycles to other processes, but your loop will still eat most of the processor's time.
What you really need is a way to put your code to sleep for a bit and set the alarm to wake it up later.
Solution
The Windows Sleep API call does exactly this. It puts your macro gently to sleep and wakes it up after a specified time. Your macro eats no processor cycles while it's snoozing. Here's how to induce slumber:
In the Declarations section of your module, include this:
' Test for 32 vs 64-bit Office: #If Win64 Then Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) #Else Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) #End If
Then in your Sub, use it like so:
Sleep 1000 ' to make your macro "sleep" for one second ( 1000 milliseconds )
See How do I use VBA code in PowerPoint? to learn how to use this example code.
Search terms:sleep,doevents,pause