

The big drawback of using Wait or Sleep, is that Excel locks you out until the wait/sleep period has finished. So to pause a macro for 5 seconds using Sleep we write this Sleep allows us to pause a macro for X milliseconds, which is a better resolution than Wait which has a minimum delay of 1 second. You can read more about these type of Declare statements and 32bit/64bit Office on Microsoft's MSDN site Note : I've tested this code in Excel 20 only. Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long) Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr) Firstly, it tells Excel where to find the function, secondly it allows us to use the 32bit version of the function in 32bit Excel, and the 64bit version of the function in 64bit Excel. This declaration statement serves two purposes.

But we can access it by using a special declaration statement in our VBA. Sleep is a Windows API function, that is, it is not part of VBA it is part of the Windows operating system. Wait does not accept delays of less than 1 second.

Or you can pause execution until a specific time e.g. This example makes the macro pause for approximately 10 seconds:Īpplication.Wait (Now + TimeValue("0:00:10")) You can use it to specify that a macro is paused for a specific period of time. Wait method is available within Excel as a VBA function, as opposed to Sleep (see below). Excel for Customer Service Professionalsĭownload the Excel Workbook.
