EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Tips VBA Wait
 

VBA Wait

Madhuri Thakur
Article byMadhuri Thakur

VBA Wait Function

VBA Wait Function

Similar to sleep function Wait function in VBA makes a code to wait or pause for a few seconds. How such functions are used that we need to make some specific code to be on pause or hold to let some other program work first and then resume the other program. It is also called as Application.Wait method in VBA.

 

 

As the name suggests VBA Wait function in VBA is used to hold a program to a specific time so that other code can work during that time. The background applications keep running and only the current module waits.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax for Wait Function in Excel VBA

The syntax to use Application.Wait function in VBA is as follows:

Syntax of Wait Function

This function returns a Boolean value.

So if we need a code to wait till 2:00 PM today the syntax for the code will be as follows:

Application.Wait (“14:00:00”)

The above code will make the code to stop until 2:00 PM and the code will resume only after 2:00 PM. This is not optimal to make a code wait for 2 PM, in general, we want to wait for a certain code for another 10 seconds and then run. If I need to make code to wait for 10 seconds the code will be as follows:

Application.Wait (Now + TimeValue (“0:00:10”))

Now is the function which will take the current time and we are adding 10 seconds to the current time through time value function so that the code can wait or pause for 10 seconds.

Note: In order to use VBA we must have developer’s tab enabled from Files tab in the options section.

How to Use Excel VBA Wait Function?

We will learn how to use a VBA Wait Excel function with few examples.

You can download this VBA Wait Excel Template here – VBA Wait Excel Template

VBA Wait Function – Example #1

Currently, the time in my laptop is 3:50 pm and I want the code to resume at 4:00 PM. We will use simple addition to some variables but display the result after 4:00 PM. We want the code to wait for that period.

Step 1: Go to the Developer tab and click on Visual Basic to open VB Editor.

VBA Wait Example 1-1

Step 2: Click on Insert Tab and then click on Module.

VBA Wait Example 1-2

Step 3: Once the code window opens up declare a sub-function to start writing the code.

Code:

Sub Sample()

End Sub

VBA Wait Example 1-3

Step 4: Declare three variables A B and C as an integer.

Code:

Sub Sample()

Dim A, B, C As Integer

End Sub

VBA Wait Example 1-4

Step 5: Assign random values to A and B variable.

Code:

Sub Sample()

Dim A, B, C As Integer 
A = 10
B = 15

End Sub

VBA Wait Example 1-5

Step 6: Use an application.wait function to make the function wait until 4:00 PM.

Code:

Sub Sample()

Dim A, B, C As Integer 
A = 10
B = 15
Application.Wait ("16:00:00")

End Sub

VBA Wait Example 1-6

Step 7: Use the addition of A and B and store the output in C.

Code:

Sub Sample()

Dim A, B, C As Integer 
A = 10
B = 15
Application.Wait ("16:00:00")
C = A + B

End Sub

VBA Wait Example 1-7

Step 8: Use a Msgbox function to display the value of C.

Code:

Sub Sample()

Dim A, B, C As Integer 
A = 10
B = 15
Application.Wait ("16:00:00")
C = A + B
MsgBox C

End Sub

VBA Wait Example 1-8

Step 9: Run the above code. Once we run the code we see that code does not display a message until 4:00 pm but after 4:00 pm I get the following output.

VBA Wait Example 1-9

I had to wait until 4:00 pm to see this result. The code of the Msgbox function had to wait actually.

VBA Wait Function – Example #2

Let us see the code with some personalized messages that the code will wait for 10 seconds and after 10 seconds have passed it will display that wait time has expired and displayed the final result.

Step 1: Go to the Developer tab and click on Visual Basic to open VB Editor.

Step 2: Declare a sub-function to start writing the code.

Code:

Sub Sample1()

End Sub

VBA Wait Example 2-1

Step 3: Declare three variables A B and C as an integer.

Code:

Sub Sample1()

Dim A, B, C As Integer

End Sub

VBA Wait Example 2-2

Step 4: Assign Random values to A and B variables.

Code:

Sub Sample1()

Dim A, B, C As Integer
A = 2
B = 10

End Sub

VBA Wait Example 2-3

Step 5: Use a Msgbox function to display that wait time is about to start.

Code:

Sub Sample1()

Dim A, B, C As Integer
A = 2
B = 10
MsgBox "The Wait Time Is Started"

End Sub

VBA Wait Example 2-4

Step 6: Use Wait function to wait for the code for 10 Seconds.

Code:

Sub Sample1()

Dim A, B, C As Integer
A = 2
B = 10
MsgBox "The Wait Time Is Started"
Application.Wait (Now + TimeValue("0:00:10"))

End Sub

VBA Wait Example 2-5

Step 7: In Variable C it stores the value of B/A and displays a personalized message that wait period is over and then display the value of C.

Code:

Sub Sample1()

Dim A, B, C As Integer
A = 2
B = 10
MsgBox "The Wait Time Is Started"
Application.Wait (Now + TimeValue("0:00:10"))
C = B / A
MsgBox "The Wait time is Over"
MsgBox C

End Sub

VBA Wait Example 2-6

Step 8: We run the code and see the first message like the following.

Result of Example 2-7

Step 9: Press OK to see the next result after 10 seconds have passed.

Result of Example 2-8

Step 10: When we press ok we get the final result.

Result of Example 2-9

In the above example, we made the code to wait for 10 seconds and then display the result of the value of C.

VBA Wait Function – Example #3

Now let us rename worksheets sheet 1 and sheet 2 as Anand and Aran Respectively but we want a gap of 5 seconds between both sheet being renamed.

Step 1: Go to the Developer tab and click on Visual Basic to open VB Editor.

Step 2: Declare a sub-function to start writing the code.

Code:

Sub Sample2()

End Sub

VBA Wait Example 3-1

Step 3: Activate the first worksheet.

Code:

Sub Sample2()

Worksheets("Sheet1").Activate

End Sub

VBA Wait Example 3-2

Step 4: Rename the first worksheet as Anand.

Code:

Sub Sample2()

Worksheets("Sheet1").Activate
Worksheets("Sheet1").Name = "Anand"

End Sub

VBA Wait Example 3-3

Step 5: Display a message that sheet 1 has been renamed and the code will pause for five seconds.

Code:

Sub Sample2()

Worksheets("Sheet1").Activate
Worksheets("Sheet1").Name = "Anand"
MsgBox "Sheet 1 renamed now code will pause for 5 Seconds"

End Sub

VBA Wait Example 3-4

Step 6: Use Wait function to make the code wait for five seconds.

Code:

Sub Sample2()

Worksheets("Sheet1").Activate
Worksheets("Sheet1").Name = "Anand"
MsgBox "Sheet 1 renamed now code will pause for 5 Seconds"
Application.Wait (Now + TimeValue("0:00:05"))

End Sub

Example 3-5

Step 7: Now rename the second sheet 2 as Aran.

Code:

Sub Sample2()

Worksheets("Sheet1").Activate
Worksheets("Sheet1").Name = "Anand"
MsgBox "Sheet 1 renamed now code will pause for 5 Seconds"
Application.Wait (Now + TimeValue("0:00:05"))
Worksheets("Sheet2").Activate
Worksheets("Sheet2").Name = "Aran"

End Sub

Example 3-6

Step 9: Display a message that sheet 2 has been renamed and the wait has been passed.

Code:

Sub Sample2()

Worksheets("Sheet1").Activate
Worksheets("Sheet1").Name = "Anand"
MsgBox "Sheet 1 renamed now code will pause for 5 Seconds"
Application.Wait (Now + TimeValue("0:00:05"))
Worksheets("Sheet2").Activate
Worksheets("Sheet2").Name = "Aran"
MsgBox "The wait time is over and sheet 2 is also renamed"

End Sub

Example 3-7

Step 10: Now run the code to see the result.

Result of Example 3-8

We can see that sheet 1 has been renamed as Anand and we get the message to wait for five seconds. Click on OK and wait for five seconds for the second message.

Result of Example 3-9

From the above examples, it is clear on how to use the Wait Function in VBA.

Things to Remember

There are a few things we need to remember about Excel VBA Wait Function:

  • It is similar to Sleep function.
  • The wait function is inbuilt in VBA unlike sleep function which is a windows function.
  • Wait function takes Time as an argument.

Recommended Articles

This has been a guide to VBA Wait Function. Here we discussed how to use Excel VBA Wait Function along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA TIMER
  2. VBA XML
  3. VBA Time
  4. VBA IsError

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Wait Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW