Pause vba for 20 seconds

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
473
Office Version
  1. 365
Platform
  1. Windows
I have the following vba code that runs every 5 minutes
Would it be possible to pause it for 20seconds after the line
.Range("BO2:CC2").Value = Application.Transpose(.Range("O5:O19").Value)
And that excel would not be frozen?
Thanks
VBA Code:
Sub test()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
   

    With Sheet1
        .Range("BO2:CC2").Value = Application.Transpose(.Range("O5:O19").Value)
    End With
    With Sheet2
        .Range("BO2:CC2").Value = Application.Transpose(.Range("O5:O19").Value)
    End With
    With Sheet3
        .Range("BO2:CC2").Value = Application.Transpose(.Range("O5:O19").Value)

       
    With Sheet1
        .Range("BO8:CC8").Value = Application.Transpose(.Range("O5:O19").Value)
    End With
    With Sheet2
        .Range("BO8:CC8").Value = Application.Transpose(.Range("O5:O19").Value)
    End With
    With Sheet3
        .Range("BO8:CC8").Value = Application.Transpose(.Range("O5:O19").Value)
  
  

    Application.OnTime Now + TimeValue("00:05:00"), "test"

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Application.Wait will lock Excel.

Better with the Timer function and a DoEvents loop so that the Excel UI stays responsive:
VBA Code:
    Dim timeout As Single
    timeout = Timer + 20
    Do
        DoEvents
    Loop Until Timer > timeout
Note - a bit more code is needed to handle Timer crossing midnight.
 
Upvote 0
Note - a bit more code is needed to handle Timer crossing midnight.
VBA Code:
Sub Pause(Optional P! = 0.01)
     D! = Timer:   F! = D + P:   DoEvents
    While Timer < F
       If Timer < D Then F = F - 86400: D = 0
    Wend
End Sub
 
Upvote 0
Or just using a Date variable :​
VBA Code:
    Dim D As Date
    D = Now + TimeValue("0:0:20")
    While D > Now:  DoEvents:  Wend
 
Upvote 0
But Excel will still not be accesible while a code is under execution !!!
 
Upvote 0
Application.Wait will lock Excel.
That is a harsh/misleading statement.

You make it sound like it 'breaks' the script.

In reality, after the 'Wait' time has elapsed, Excel execution continues normally.
 
Upvote 0
VBA Code:
Sub Pause(Optional P! = 0.01)
     D! = Timer:   F! = D + P:   DoEvents
    While Timer < F
       If Timer < D Then F = F - 86400: D = 0
    Wend
End Sub
timer does not need to go past midnight, but adding code in middle of my existing code wont work,
and Application.Wait (Now + TimeValue("0:00:20")) does lock excel.
 
Upvote 0
So you must try to clearly elaborate … As usually this kind of code does not need any pause …​
 
Upvote 0
So you must try to clearly elaborate … As usually this kind of code does not need any pause …​
thanks for everyones help, problem solved, will just run to different macros
 
Upvote 0

Forum statistics

Threads
1,217,358
Messages
6,136,093
Members
449,991
Latest member
IslandofBDA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top