Wait Fo Rprogram to Finish Running Before Continuing Vba

Wait and Sleep functionality in programs is required to hold or pause the program execution for a specified time. These methods suspend all the activities of Microsoft Excel and sometimes may also prevent you from performing other operations on it until the pause is in effect.

However, your computer can simultaneously process other background tasks like printing or thread ordering.
WAIT & SLEEP Functions IN VBA

Significance of Wait and Sleep Functions in VBA:

These functions can be quite useful when you need to hold the program to wait for some other process or task (not directly connected or communicable to the VBA engine) that is yet to be completed.

In such cases, you can find out the maximum time required for completing such a task, and then in your code, you hold the execution for that amount of time.

For example, We have a VBA code that can run some other executable (.exe) and after running that executable you need to wait for that executable to complete and then continue the code. So, in such a case we know that VBA cannot directly communicate with the executable file.

Hence we can code in such a way that first, we start that executable then let the code wait for 10 seconds (max time required for this executable to run), and then continue the execution again.

VBA Wait Function in Excel:

The WAIT is a VBA function only available in Excel. Its syntax is as follows:

Here 'Time' specifies the time at which you want the macro to resume again. 'Time' should always be in Microsoft excel time format.

Let's see some examples of Wait Function in VBA:

Example 1: Pausing a code till 2.00 PM today.

            Sub            WaitTest()
MsgBox ("This application is started!")
Application.Wait "14:00:00"
MsgBox ("Excecution resumed after 2PM")
End Sub

Example 2: Pausing an application for 10 seconds.

            Sub            WaitTest()
MsgBox ("This application is started!")
Application.Wait (Now + TimeValue("0:00:10"))
MsgBox ("Excecution resumed after 10 Seconds")
End Sub

Example 3: Using the VBA Wait function to create a program that tells time after every minute (till 10 loops).

            Public            Sub            TalkingTime()
For i = 0 To 10
Application.Wait (Now + TimeValue("0:01:00"))
Application.Speech.Speak ("The Time is" & Time)
Next i
End Sub

Now let's move to Sleep Function.

Sleep Function:

Sleep is a windows function and not a VBA Function, but you can still use this function in VBA code by calling the windows Sleep API. Actually sleep is a function present inside Windows DLL files. So, before using them you have to declare the name of API above the code in your module.

The syntax of the Sleep statement is as follows:

Here, 'delay' specifies the time in milliseconds till which you have to pause the execution.

Let's see some examples of Sleep Function in VBA:

Example 1: Pausing an application for 10 seconds

            #If VBA7 Then            
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds as Long) 'For 32 Bit Systems
#End If
Sub SleepTest()
MsgBox "Execution is started"
Sleep 10000 'delay in milliseconds
MsgBox "Execution Resumed"
End Sub

Example 2: Halting the code for a user-defined delay by using an InputBox function.

            Sub            SleepTest()
On Error GoTo InvalidRes
Dim i As Integer
i = InputBox("Enter the Seconds for which you need to pause the code :")
Sleep i * 1000 'delay in milliseconds
MsgBox ("Code halted for " & i & " seconds.")
Exit Sub
InvalidRes:
MsgBox "Invalid value"
End Sub

Difference between VBA Wait and Sleep Function:

The job of both of these functions is the same but the sleep function is not as accurate as Wait. Sleep statement depends on the processor's ticks to calculate the time delays which may vary slightly on different machines. But this is not the case with Wait Function.

The advantage of the Sleep statement over Wait is that it is quite flexible as you can give the time delays in milliseconds. While in the Wait function you can only delay the application by whole seconds.

So, this was all about VBA Wait and Sleep Functions. Do let us know in case you have any queries related to the topic.

hallcomens.blogspot.com

Source: https://exceltrick.com/formulas_macros/vba-wait-and-sleep-functions/

0 Response to "Wait Fo Rprogram to Finish Running Before Continuing Vba"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel