To pause an Excel VBA macro on Windows, declare the Windows API function Sleep in a standard VBA module, then call it with a duration in milliseconds, such as Sleep 1000 for about one second. Sleep is not a built-in VBA command, and while it runs Excel may stop responding to clicks and keyboard input.
What Sleep does in Excel VBA
Sleep is a Windows function that pauses the thread running your macro for a requested number of milliseconds. The declaration connects VBA to the function in the Windows system library, kernel32. The call itself is simple: a value of 250 requests a quarter of a second, while 2000 requests two seconds.
Sleep can be useful when a macro needs to pause briefly between actions, for example while an external program or device has time to respond. It does not, however, make Excel “wait intelligently” for a particular condition. A fixed pause cannot guarantee that a file has finished downloading, a process has completed, or a calculation has settled. For those tasks, a check for the actual condition is usually more dependable than guessing a delay.
Also distinguish a pause from a VBA calculation or a timer that schedules later work. Sleep suspends the macro that called it. It does not create a background task, and it does not make VBA run simultaneously with the rest of your code.
Declare Sleep for 32-bit and 64-bit Office
In current Excel for Microsoft 365 on Windows, place this declaration at the top of a standard module, before any Sub or Function procedures. The conditional compilation block also includes a declaration for older VBA versions that predate VBA7.
#If VBA7 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
The PtrSafe keyword is required for API declarations in VBA7, which is used by both 32-bit and 64-bit modern Office. The argument remains Long because the Windows API parameter is a 32-bit millisecond value; it is not a pointer. Do not change it to LongPtr just because Office is 64-bit.
Use a standard module rather than placing the declaration inside a worksheet or workbook object module. Because the declaration is marked Private, code in that module can call it; if a different module also needs Sleep, put an appropriate declaration there or centralize the calls in a helper procedure.
A basic pause macro
With the declaration in place, this procedure pauses for one second. The value is expressed in milliseconds, so 1000 is approximately 1000 milliseconds, or one second.
Public Sub PauseOneSecond()
Dim delayMs As Long
' Request a pause of one second.
delayMs = 1000
Sleep delayMs
End Sub
Save a copy of the workbook before running a macro. VBA actions generally cannot be reversed with Ctrl+Z, and a pause does not change that. To run the example, open the Visual Basic Editor from the Developer tab, insert a standard module if needed, add the declaration and procedure, then run PauseOneSecond from the editor or Excel’s Macro dialog. Excel for the web cannot run VBA macros.
A pause can also be placed between repeated operations. For instance, a macro might process a small batch, pause briefly, and then continue. Keep the pause short and purposeful: adding a one-second delay to each of 500 iterations adds at least 500 seconds to the run, even if the underlying work is fast.
Milliseconds do not mean exact timing
The argument to Sleep is measured in milliseconds, but that does not guarantee that VBA resumes at exactly the requested instant. Windows schedules threads, and the macro may resume later than requested if the system is busy or the scheduling interval does not align with the requested duration. Treat Sleep as a minimum-style pause request, not as a precision clock.
For ordinary automation, a few hundred milliseconds may be enough to create a visible pause or give another component a short opportunity to respond. For precise measurement, use a clock or timer designed for measurement rather than inferring elapsed time from how long a Sleep call requested. Very long pauses are usually a poor design: they can leave a user unsure whether Excel has frozen and make interruption difficult.
The parameter is a signed 32-bit VBA Long. Avoid passing negative values, and do not assume a huge value is a practical way to schedule a macro for much later. If the task must resume at a specific future time, use a scheduling approach instead of leaving Excel’s macro thread asleep.
Excel responsiveness and DoEvents
While the macro’s thread is sleeping, Excel’s interface may appear unresponsive. The workbook may not repaint promptly, and the user may be unable to interact normally until the call returns. That is the main practical drawback of Sleep in a long-running macro. A brief pause is different from a long wait, but neither should be used as a substitute for designing work that can be cancelled or monitored.
DoEvents gives Windows an opportunity to process pending events before VBA continues. It can be useful in a loop that performs work in small chunks, but it is not a replacement for Sleep and it does not make a sleeping call responsive. Calling DoEvents also has a re-entrancy risk: another event or user action may start code while the original procedure is partway through. That can cause overlapping work or unexpected changes to shared data.
If a loop uses both, use them deliberately: process a small unit of work, allow pending events to run if that is safe, and then make any necessary short pause. Do not assume that inserting DoEvents everywhere is harmless. Consider whether the macro can be run twice, whether a button can be clicked again, and whether event procedures might alter the same cells while the original code is paused.
Sleep versus Application.Wait
Application.Wait is Excel’s built-in way to pause a procedure until a specified time. It avoids a Windows API declaration and is often a simpler choice when a delay of roughly a second or longer is suitable. It is not a millisecond-precision alternative: it is intended for waiting until a time, and Excel pauses its activity while waiting.
For example, a procedure can use Application.Wait Now + TimeValue("00:00:02") to wait until about two seconds after the current time. This expression is suitable for a simple whole-second delay. If the requirement is a very short pause, Sleep accepts a millisecond value, although actual resume timing is still subject to Windows scheduling.
Choose based on the real requirement rather than habit:
- Use
Sleepwhen the macro is Windows-only and needs a short, millisecond-based pause request. - Use
Application.Waitwhen a straightforward time-based wait is sufficient and avoiding a Windows API declaration matters. - Use neither when the macro should wait for a specific file, process, or data condition; check for that condition instead.
When a fixed pause is useful—and when it is not
A fixed delay can be reasonable when another component needs a brief settling period and there is no reliable completion signal available. A macro that sends a command to a simple external interface, for example, might pause before sending the next command. The duration should be based on the interface’s requirements, and the code should still handle the possibility that the other component takes longer than expected.
A pause is a weak solution for waiting for workbook calculations, refreshed data, or a file operation to finish. The time needed can vary with workbook size, network conditions, machine load, and the source of the data. A delay that works once may fail on a slower computer or a busy connection. Where possible, inspect the relevant state, use a completion event, or retry a check with a sensible timeout. That way, the macro responds to what actually happened rather than relying on an arbitrary number of milliseconds.
Do not use Sleep as a way to slow down Excel’s recalculation or as a substitute for controlling calculation settings. If the concern is that a result is not ready, identify which calculation or external refresh is still pending and wait for that work to complete using the appropriate mechanism for that operation.
Common problems and fixes
- “Sub or Function not defined”: Check that the API declaration appears before procedures in a standard module, and that the procedure name is spelled
Sleep. If the declaration is private in another module, code outside that module cannot call it directly. - Compile error about PtrSafe: The declaration may be missing the VBA7 conditional block or may have been copied into an older VBA environment without the legacy branch. Use the complete conditional declaration above for compatibility with VBA7 and older VBA.
- Excel appears frozen: The macro is likely still inside Sleep or doing other work. Shorten the pause, show progress between chunks, or use a condition-based approach. A sleeping macro is not necessarily a crashed application.
- The pause seems longer than requested: Windows scheduling and system load can delay resumption. Do not rely on Sleep for exact timing or use it to enforce a precise rate without measuring elapsed time.
- The code does not work on a Mac:
kernel32is a Windows library, so this declaration is Windows-specific. Use a platform-neutral VBA approach such as Application.Wait when its timing is adequate, or create separate platform-specific code.
Windows, Mac, and Excel for the web
This API example is for Excel for Microsoft 365 on Windows, whether Office is 32-bit or 64-bit. It is not portable unchanged to Excel for Mac: macOS does not provide the Windows kernel32 library. A macro that must run on both Windows and Mac should avoid this declaration or branch by platform and provide a suitable implementation for each system. Application.Wait is often a simpler cross-platform option for coarse pauses, but it still pauses Excel activity and does not provide exact millisecond timing.
Excel for the web does not execute VBA. To run VBA, open the workbook in the desktop Excel application with macros enabled. If the workflow must run in a browser, it needs a different automation approach, such as Office Scripts where appropriate; VBA code and Windows API calls cannot simply be run from the web version.
Frequently Asked Questions
Is Sleep built into VBA? No. Sleep is a Windows API function, not a native VBA statement. On Windows, declare it in a standard module before calling it. For a built-in Excel pause, consider Application.Wait when its coarser time-based behavior fits.
How do I pause VBA for one second? With the Windows Sleep declaration available, call Sleep 1000. The argument is milliseconds, and the actual pause may be slightly longer because Windows does not guarantee exact resumption timing.
Does Sleep keep Excel responsive? Not reliably. Sleep suspends the calling thread, so Excel may stop responding while the macro waits. DoEvents can process pending events before a later pause, but it does not make Sleep itself responsive and can allow other code to run.
Can I use Sleep in 64-bit Excel? Yes, on Windows, with a VBA7 declaration containing PtrSafe. The parameter should remain a VBA Long because the Windows function takes a 32-bit millisecond value.
What should I use instead of Sleep on Mac? The Windows API declaration cannot run on Mac. For a simple, relatively coarse delay, Application.Wait is a more portable VBA option. For a task that must wait for a real event, check for that event rather than substituting a fixed pause.
Related
- 2 Criteria Conditional Formatting in Excel: Two Methods
- Power Query: What It Contains and How It Works
- Insert a Row in a QuickBooks Journal Entry Correctly
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Sort Method of Range Class Failed: Fixes“Sort method of Range class failed” is usually Excel’s run-time error 1004: a VBA sort request could not…
- Find a Z-Score in Excel VBA: A Simple Step-by-Step GuideTo find z-scores with Excel VBA, calculate the mean and standard deviation of your data, then use the…
- Excel VBA Data Entry Form With a Picture Box: How It WorksYes. In Excel for Microsoft 365 on Windows, make a VBA UserForm, add an Image control to act…
- Can You Make Macros in Google Sheets? How They WorkYes. You can make macros in Google Sheets by recording a sequence of actions in the browser; Sheets…
- VBA Macro Error Handling: Patterns, Examples, and FixesVBA macro error handling lets a procedure respond to runtime problems instead of stopping without explanation. Use On…
- VBA Dictionary Count: How to Get the Number of KeysTo get the number of keys in a VBA Dictionary, read its Count property: dict.Count. It returns the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.