> ## 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 Your VBA Code For a Set Amount of Time
- URL: https://nolongerset.com/how-to-pause-your-vba-code/
- Published: 2021-08-10T02:26:34.000Z
- Updated: 2026-05-08T12:58:24.000Z
- Description: There is no built-in function to pause code in VBA. The easiest and safest way to do it is to use the Sleep API function. Here's a quick how-to.
- Author: Mike Wolfe
- Tags: Basic, #Import 2026-05-20 02:59

The most efficient way to pause execution of your VBA code is to use the [Windows Sleep API function](https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep):

> Suspends the execution of the current thread until the time-out interval elapses.

## Usage

### Declaring the function

As with all Windows API functions, before you can call the function from VBA you need to declare it using the [VBA Declare statement](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/declare-statement). 

The following code should be placed in the header section of a standard module. By including the Public keyword, you can call the function from any other module in your project.

```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
```

### Calling the function

Whenever you want to pause execution of your code for a few seconds, you simply call `Sleep` and pass it the number of **milliseconds** you want your code to wait. For example, to wait for 5 seconds, you would pass 5000 (1 second = 1,000 milliseconds):

```vba
Sleep 5000   'Pause execution for five seconds
```

### Maximum compatibility

The above code works in all versions of Access from 95 through 365/2019, in both 32-bit and 64-bit flavors.

The [**PtrSafe** keyword](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/ptrsafe-keyword) is what allows the call to work in both 32-bit and 64-bit environments. This is especially important now that [64-bit is the default installation mode](https://nolongerset.com/64-bit-now-the-default/) beginning with Access 2019/365.

However, the **PtrSafe** keyword was not introduced until VBA 7\. Thus, to avoid [compile errors](https://nolongerset.com/compile-errors/) in older VBA versions, we include the [conditional compilation directive](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/ifthenelse-directive) `#If VBA7`. Lines with a leading pound sign (`#`) are evaluated before the code is compiled.

The following code would raise a compile error in older versions of VBA (MS Access 2003 and earlier) because of the unsupported **PtrSafe** keyword:

```vba
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
```

This code is not safe in MS Access 2003 and earlier.

For more information about 32-bit and 64-bit VBA compatibility, refer to Philipp Stiefel's [authoritative article](https://codekabinett.com/rdumps.php?Lang=2&targetDoc=windows-api-declaration-vba-64-bit) on the topic.

---

### External references

[Sleep function (synchapi.h) - Win32 appsSuspends the execution of the current thread until the time-out interval elapses.Microsoft Docskarl-bridge-microsoft![](https://docs.microsoft.com/en-us/media/logos/logo-ms-social.png)](https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep)

[Declare statement (VBA)Learn how to use Declare statement (VBA)Microsoft Docso365devx![](https://docs.microsoft.com/en-us/media/logos/logo-ms-social.png)](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/declare-statement)

[PtrSafe keyword (VBA)Microsoft Docso365devx](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/ptrsafe-keyword)

[If...Then...Else directiveMicrosoft Docso365devx](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/ifthenelse-directive)

[Windows API declarations in VBA for 64-bitHow to convert your API Declarations to 64-bit. - Common myths debunked, key factors explained!CodekabinettPhilipp Stiefel![](https://codekabinett.com/images/mvp_hor.png)](https://codekabinett.com/rdumps.php?Lang=2&targetDoc=windows-api-declaration-vba-64-bit)

### Referenced articles

[Compile ErrorsCompile errors are easy to keep out of deployed code (just remember to Debug > Compile). Which is good because they hard crash Access in Runtime mode.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/08/volkswagen-4220406_1920.jpg)](https://nolongerset.com/compile-errors/)

[Office 2019 Runs in 64-bit Mode By Default. Here’s What That Means for VBA DevelopersThrough Office 2016, default installs used the 32-bit version of the software. Now that 64-bit is the default, it’s time to bite the bullet and convert your VBA code.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2021/07/vacations-1569608_1920.jpg)](https://nolongerset.com/64-bit-now-the-default/)

*Image by [PublicDomainPictures](https://pixabay.com/users/publicdomainpictures-14/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=22194) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=22194)*