Written by Allen Wyatt (last updated January 16, 2023)
This tip applies to Excel 2007, 2010, 2013, and 2016
Chris is developing an Excel macro that displays a series of items to the user. He would like to introduce a delay in the macro of, say, one second between each piece of information he displays, and he wonders if there is an easy way to add such a delay.
There are a few ways you can introduce a delay into your macro. The traditional way is to use the Wait method, which is used with the Application object. You use it to introduce a one-second delay in this manner:
Application.Wait (Now() + TimeValue("0:00:01"))
Note that the parameter required by the Wait method is the time at which you want your macro to resume running. In other words, it is the "wait until" time. That is why the above example uses the current time (from the Now function) incremented by a single second.
If you prefer to not use the Wait method (for whatever reason), you can also just use a loop to bide your time:
Dim WaitTime As Date WaitTime = Now() + TimeValue("0:00:01") Do While Now < WaitTime Loop
A variant on this approach relies on the Timer function, which returns the number of seconds since midnight:
Dim Endpoint as Single Endpoint = Timer + 1 Do While Timer < Endpoint Loop
When using a loop in this manner, it probably won't be a big issue if you are delaying for only a second. If you need to delay for a longer time, you'll probably want to place the DoEvents function into the loop:
Dim Endpoint as Single Endpoint = Timer + 1 Do While Timer < Endpoint DoEvents Loop
The reason is simple—if you don't, then Excel won't respond to any other events while in the loop. This can make it seem like your system has frozen or hung while waiting.
If you need more granularity on your delays (down to the millisecond range), you may want to rely on the Sleep function, which is part of the Windows API. (It is not a part of VBA.) In order to use it, you'll need to include a declaration at the beginning of your module, in the declarations area, before any procedures:
#If VBA7 And Win64 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
Then, within your macro you can use the Sleep function in this manner:
Sleep(1000)
This pauses the system for 1000 milliseconds, which is 1 second.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5134) applies to Microsoft Excel 2007, 2010, 2013, and 2016.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
When you have a macro that processes a huge amount of data, it can seem like it takes forever to finish up. These ...
Discover MoreIf you need to exit a macro before it is finished running, you can do it using a brute force method, or you can build in ...
Discover MoreYou can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-01-23 17:09:15
Daryl D
We can use Excel.Application.Wait in Access if you reference Microsoft Excel 16.0 Object Library .
See sub below:
Sub AppWait()
'must first reference Microsoft Excel 16.0 Object Library
Dim s As String: s = "hello"
Excel.Application.Wait (Now() + TimeValue("0:00:05"))
MsgBox s
End Sub
2022-01-23 17:04:47
Daryl D
We can use Excel.Application.Wait if you reference Microsoft Excel 16.0 Object Library .
See sub below:
Sub AppWait()
'must first reference Microsoft Excel 16.0 Object Library
Dim s As String: s = "hello"
Excel.Application.Wait (Now() + TimeValue("0:00:05"))
MsgBox s
End Sub
2022-01-20 14:36:18
J. Woolley
@Chester Hood
I don't believe either Word or Access supports Application.Wait, but the other methods should work. Word and Excel (but not Access) support Application.OnTime, which provides another approach. Access supports Form.OnTimer. For an example using Application.OnTime, see
https://excelribbon.tips.net/T008192_Forcing_a_Workbook_to_Close_after_Inactivity.html
2022-01-19 17:03:31
Chester Hood
Will the VBA suggestions work in Word and Access?
2022-01-19 05:26:24
Super_James
I have one question. Disregarding "Sleep", aren't the other methods problematic when the delay is for one second?
I haven't tried it, but I could see where the delay may range from 1.99 seconds to .01 seconds. I'm I missing something.
The range thing wouldn't be as big a deal if the necessary delay were eight seconds, for example.
2017-12-25 06:20:20
Willy Vanhaelen
For current use you don't need the granularity down to miliseconds, you can use this macro for which you don't need to declare API functions:
Sub Sleep(s As Integer)
Application.Wait Now + TimeSerial(0, 0, s)
End Sub
You use it this way for a 5 seconds delay
call Sleep( 5) or simply Sleep 5
Or even for a half second:
Sleep 0.5
2017-12-23 09:30:37
jean-pierre degroote (aka JP Ronse)
Hi Allen,
Useful tips but all have the disadvantage that they stop/delay the execution of the macro where it can be a requirement that some further code need to be executed.
See discussion: https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_winother-mso_2007/vba-macros/7d4f6b0d-0a59-4810-a09c-23d8cbb24a8e
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments