> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Pause VBA Code
- URL: https://nolongerset.com/how-to-pause-vba-code/
- Published: 2022-01-15T01:11:11.000Z
- Updated: 2026-05-08T13:00:12.000Z
- Description: A simple Windows API call makes for a reliable and efficient way to pause your VBA code. Much better than a "do-nothing loop."
- Author: Mike Wolfe
- Tags: Basic, #Import 2026-05-20 02:59

The simplest way to add a pause to your VBA code is with the Windows [Sleep API](https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep).

```vba
#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
```

## Sample Usage

As this is an API call, it has to be declared in the header section of a module. 

I generally declare it as Public in the header section of a module that I include in every application (for me, that's my error logging module). 

Here's what it looks like in context. Note that the amount of time to sleep is given in *milliseconds*, not seconds. So, if you want to pause your code for three seconds, you would call `Sleep 3000`.

```vba
Option Explicit

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub ThreeSecondSleepTest()
    Debug.Print "Start: "; Now
    
    Sleep 3000   'Pause for 3,000 milliseconds (i.e., 3 seconds)
    
    Debug.Print "End:   "; Now
End Sub
```

![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2022/01/image-37.png)

While your code is sleeping, msaccess.exe relinquishes its use of the processor so that other processes may execute their code. This is a much more efficient and reliable way to pause your code than ugly hacks like a long-running [DoEvents](https://nolongerset.com/demystifying-doevents/) loop.

*Image by [愚木混株 Cdd20](https://pixabay.com/users/cdd20-1193381/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1180921) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=1180921)*