• Skip to primary navigation
  • Skip to main content
  • Skip to footer
WallStreetMojo

Wallstreet Mojo

Wallstreet Mojo

MENUMENU
  • Resources
        • Excel

          • Excel Functions
          • Excel Tools
          • Excel Tips
        • Excel
        • Financial Functions Excel

          • NPV in Excel
          • IRR in excel
          • PV in Excel
        • Financial-Functions-Excel
        • Lookup Functions Excel

          • VLOOKUP
          • HLOOKUP
          • Index Function
        • Lookup-Functions-in-Excel
        • Excel Charts

          • Pareto Chart in Excel
          • Gannt Chart in Excel
          • Waterfall Chart in Excel
        • Excel-Charts
        • VBA

          • VBA Left Function
          • VBA Paste Special
          • VBA Worksheet Function
        • VBA
        • Others

          • Resources (A to Z)
          • Financial Modeling
          • Equity Research
          • Private Equity
          • Corporate Finance
          • Financial Statement Analysis
  • Free Courses
  • All Courses
        • Certification Courses

          Excel Course certificate
        • Excel VBA All in One Bundle

          Excel-VBA-Certification-Course
        • Excel Data Analysis Course

          Excel-Data-Analysis-Course
        • VBA Macros Course

          VBA-Training-Course
        • Others

          • Basic Excel Training
          • Advanced Excel Course
          • Tableau Certification Course
          • Excel for Finance Course

          • Excel for Marketers Course
          • Excel for HR Managers
          • Excel for Power Users
          • View All
  • Excel VBA All in One Bundle
  • Login

VBA Pause

Home » VBA » VBA Tips » VBA Pause

By Tejswini Bhosale Leave a Comment

vba-pause

In VBA very similar to what we do in a sleep command we can also pause our code for a specified time to execute, why do we pause our code to execute because we give enough time to the process before to have enough time to execute first and to pause a code in VBA we use application.wait method.

Pause VBA Code

When we build large VBA projects after performing something we may need to wait for some time to do other tasks. In such scenarios how do we pause the macro code to do our task? We can pause the VBA code for a specified time period by using two functions in VBA and those functions are “Wait” & “Sleep”.

In this article, we will show you how to pause the VBA code while running.

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

How to Pause the VBA Code Using Wait Method?

“Wait” is the function we use in VBA to hold the macro running for a specific amount of time. By applying this function we need to mention until what time our code should wait.

For an example, if you are executing the code at 13:00:00 if you supply the time as “13:15:00” then it will hold the macro running for 15 minutes.

Now, look at the argument of WAIT function.

vba pause syntax

In time argument we need to mention at what time our code should pause or wait.

For an example look at the below VBA excel code.

Code:

Sub Pause_Example1()

Range("A1").Value = "Hello"
Range("A2").Value = "Welcome"

Application.Wait ("13:15:00")

Range("A3").Value = "To VBA"

End Sub

vba pause example 1.1

Remember while running this code my system time is 13:00:00, as soon as I run the code it will execute the first two lines i.e.

Range("A1").Value = "Hello” &

Range("A2").Value = "Welcome"

But if you look at the next line it says Application.Wait (“13:15:00”), so after executing those lines tasks my macro will be paused for 15 minutes i.e. from 13:00:00 it will wait until my system time reaches 13:15:01.

Once my system time reaches that time it will execute the remaining lines of code.

Range("A3").Value = "To VBA"

However, this is not the best way of practicing the pause code, let’s say you are running the code at different time, then we need to use NOW VBA function with TIME VALUE function.

Popular Course in this category
Cyber Monday Sale
VBA Training (3 Courses, 12+ Projects) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
4.6 (247 ratings)
Course Price

View Course

Related Courses

Now function returns the current date & time as per the system we are working on.

TIME Value function holds the time from 00:00:00 to 23:59:29.

Ok, assume we need to pause the code for 10 minutes whenever we run the code, then we can use below code.

Code:

Sub Pause_Example1()

Range("A1").Value = "Hello"
Range("A2").Value = "Welcome"

Application.Wait (Now() + TimeValue("00:00:10"))

Range("A3").Value = "To VBA"

End Sub

vba pause example 1.2

This is similar to previous code but the only difference is we have added NOW & TIME VALUE function.

Whenever we run the this VBA code it will hold or pause the execution for 10 minutes.

How to Pause the VBA Code Using Sleep Method?

Sleep is a complicated thing in VBA because it is not a built-in function. Since it is not a built-in function in order to make it available to use we need to add the below code to the top of our module.

Code:

#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)
#End If
'For 32 Bit Systems

You just have to copy the above code and paste at the top of the module.

vba pause example 2.1

The reason why we need to add the above code because SLEEP is a VBA function presented in Windows DLL files, so we need to declare the nomenclature before we start the subprocedure.

Ok, let’s look at the example of SLEEP function now.

Code:

Sub Pause_Example2()

Dim StartTime As String
Dim EndTime As String
StartTime = Time

MsgBox StartTime

Sleep (10000)

EndTime = Time

MsgBox EndTime

End Sub

example 2.2

Now I will run the code first we have declared two variables as String.

Dim StartTime As String

Dim EndTime As String

Then we have assigned the TIME function to the StartTime variable. TIME function returns the current time as per system.

StartTime = Time

Then we have assigned the same to show in the message box.

MsgBox StartTime

Then I have applied SLEEP function as Sleep (10000).

Here 10000 is milliseconds which is equal to 10 seconds in VBA.

Then, at last, I have assigned the one more TIME function to the variable EndTime.

Now again I have written a code to show the time.

EndTime = Time

This will show the difference between start time and end time.

Now I will execute the code and see what the start time is.

example 2.3

When I execute the code my system time is 13:40:48 and now my code will sleep for 10 seconds. At the end time, my time is as follows.

example 2.4

So, like this, we can pause the code from executing it for a specified amount of time.

Recommended Articles

This has been a guide to VBA Pause Method. Here we discuss how to pause VBA code using SLEEP and WAIT function in Excel VBA with examples. You can learn more about VBA from the following articles –

  • VBA Switch Case | Examples
  • FileDialog Option in VBA
  • VBA InStr Function
  • GetOpenFilename in VBA
  • VBA Operators
  • VBA COUNTIF Examples
  • VBA Now Function
  • VBA Val Function
  • Create VBA Progress Bar
6 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>

Filed Under: VBA, VBA Tips

Reader Interactions
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Footer
COMPANY
About
Reviews
Blog
Contact
Privacy
Terms of Service
FREE COURSES
Free Finance Online Course
Free Accounting Online Course
Free Online Excel Course
Free VBA Course
Free Investment Banking Course
Free Financial Modeling Course
Free Ratio Analysis Course

CERTIFICATION COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Business Valuation Course
Equity Research Course
CFA Level 1 Course
CFA Level 2 Course
Venture Capital Course
Microsoft Excel Course
VBA Macros Course
Accounting Course
Advanced Excel Course
Fixed Income Course
RESOURCES
Investment Banking
Financial Modeling
Equity Research
Private Equity
Excel
Books
Certifications
Accounting
Asset Management
Risk Management

Copyright © 2019. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.
WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

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

WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

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

WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

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

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

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

WallStreetMojo

Download VBA Pause Excel Template

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Limited Period Offer - VBA Training Course (6 courses, 35+ hours video) View More