Excel VBA Sleep Function
VBA Sleep function is a windows function present under windows DLL files which is used to stop or pause the macro procedure from running for a specified amount of time after that certain amount of we can resume the program.
There are situations where we need to pause our macro running process to complete other sets of tasks. Other sets of tasks could be part of our coding or part of other macro procedure, or it could be input for the current excel macroExcel MacroMacro in excel is a series of instructions in code that helps automate manual tasks, thereby saving time. Excel executes those instructions in a step-by-step manner on the given data. . How can you pause the program when it is running? We can pause the code of procedure for some time specified by the user, and after that certain amount of we can resume the program. We can do this in VBA by using the SLEEP function.
What Does VBA Sleep Function Do?
SLEEP, as the name itself says, “sleep for some time,” “rest for some time,” “pause for a time,” time off for some time,” etc.. The sleep function allows users to pause our macro code for milliseconds. Using this, we can delay the process of macro code.
If you think we have a built-in function called SLEEP, then you are wrong because, in VBA, there is no such function. Rather, we have a function called Sleep as a windows function. By entering a special set of codes, we can actually call this function in VBA. In fact, it is a function present inside Windows DLL files, so we need to declare the nomenclature of API before the start of the subroutine in VBASubroutine In VBASUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA..
Below is the VBA codeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task..
#If VBA7 Then Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64-Bit versions of Excel #Else Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32-Bit versions of Excel #End If
Copy the above and paste in your module before you start writing the macro codes. It should be pasted like this in your module.
Before I show you the way of writing the code, let me tell you about the sleep function a little more. It delays the process in milliseconds. So 1 second is equal to 1000 milliseconds, if you want to pause for 10 seconds, it should be 10000 milliseconds.
Once the API code pasted before the start of the Sub procedure, create a macro name.
#Sub Sleep_Example1() End Sub
Declare two variables as a string.
Dim StartTime As String Dim EndTime As String
For the StartTime variable, assign the value of the TIME function. Note: TIME in excelTIME In ExcelTime is a time worksheet function in Excel that is used to calculate time based on the inputs provided by the user. The arguments can take the following formats: hours, minutes, and seconds. function returns the current time.
StartTime = Time
Now we will display this message in the message box VBAMessage Box VBAVBA MsgBox function is an output function which displays the generalized message provided by the developer. This statement has no arguments and the personalized messages in this function are written under the double quotes while for the values the variable reference is provided..
StartTime = Time MsgBox StartTime
Now we will pause the code for 10 seconds using the sleep function. As I told, it pauses the code in milliseconds, so to pause for 10 seconds. We need to use 10000 milliseconds.
Sub Sleep_Example1() Dim StartTime As String Dim EndTime As String StartTime = Time MsgBox StartTime Sleep (10000) End Sub
Now use the second variable, EndTime, and assign the current time.
Sub Sleep_Example1() Dim StartTime As String Dim EndTime As String StartTime = Time MsgBox StartTime Sleep (10000) EndTime = Time MsgBox EndTime End Sub
Now two variables StartTime and EndTime, will hold macro beginning time and ending time. Run this macro; at first, we will see macro starting time, i.e., current time in your system.
Click on OK. It will sleep for 10 seconds. You can see the buffer symbol.
After 10 seconds, it will start to resume the code so it will show the end time, i.e., after waiting for 10 seconds, what’s the current time now.
Now you can see macro started at 10:54:14 and ended at 10:54:24, i.e., Exactly the 10-second difference is there. In those 10 seconds, VBA pauseVBA PauseVBA Pause helps to pause a code from executing for a particular period. You can pause the VBA code for a specific period by using two functions, and those functions are “Wait” & “Sleep.” the code running.
Example #2 – Sleep Function in Loops
Sleep is best used with loops in VBA. For example, I want to insert serial numbers from 1 to 10 using Do while loop in VBALoop In VBALoops are commonly used in all of the programming languages, where there is a certain need or a criteria when we need a certain code to run a certain times. In VBA, there are several different types of loops, including Do while, Do till, For Loop, and For each loop. These loops help code to execute until the condition is met..
After inserting the one number, my code should wait for 3 seconds, so when the loop runs 10 times, it should be 30 seconds in total.
Sub Sleep_Example2() Dim k As Integer k = 1 Do While k <= 10 Cells(k, 1).Value = k k = k + 1 Sleep (3000) '1000 milliseconds is 1 second so 3000 is equal to 3 seconds Loop End Sub
Run this code, and you have to wait for a minimum of 30 seconds to complete the process.
To track the exact time, use the below code.
Sub Sleep_Example2() Dim k As Integer Dim StartTime As String Dim EndTime As String StartTime = Time MsgBox "Your Code Started at " & StartTime k = 1 Do While k <= 10 Cells(k, 1).Value = k k = k + 1 Sleep (3000) '1000 milliseonds is 1 second so 3000 is equal to 3 seconds Loop EndTime = Time MsgBox "Your Code Ended at " & EndTime End Sub
This code will display you 2 message boxes, the first one will show the starting time, and the second one will show the end time.
Note: While running this code, you cannot use excel. Even the escape key will not work.
This has been a guide to VBA Sleep Function. Here we discuss how to use Sleep in Excel VBA along with a practical example and downloadable excel template. Below are some useful excel articles related to VBA –