I am trying to insert a pause into Excel VBA code. The following code generates a compile error: "Invalid Qualifier".
The function Pause() is trying to combine the Application.Wait method with DoEvents to get the best of both worlds; Excel will continue to process keystrokes and mouse clicks while not eating up CPU resources as it waits.
Background:
http://www.fmsinc.com/microsoftaccess/modules/examples/avoiddoevents.asp http://www.cpearson.com/excel/WaitFunctions.aspx
http://vbadud.blogspot.com/2008/03/sleep-function-in-excel-vba.html
Public Declare Function GetTickCount Lib "kernel32" () As Long
' A DoEvents loop uses CPU power.
' Application.Wait suspends all keyboard and mouse actions.
' This function is a hybrid to combine the best of both worlds.
Public Sub Pause(Optional Timeout As Single = 5)
Dim EndTime
EndTime = GetEndTick(Timeout)
Do While GetTickCount() <= EndTime
DoEvents
Application.Wait DateAdd("s", 0.1, Now)
Loop
End Sub
' Timeout is in seconds.
Function GetEndTick(Timeout)
GetEndTick = GetTickCount() + Timeout * 1000
End Function