Time Loop

bongobus

New Member
Joined
Jul 11, 2017
Messages
28
I would like a looped code that waits 2 seconds then refreshes the links e.g calculate then waits 2 seconds then refreshes - basically a loop
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi,

try the "sleep"-function:

Code:
Private Declare PtrSafe Sub Sleep Lib "kernel32.dll" ( _
    ByVal dwMilliseconds As Long)

Public Sub mySleep()
   'your code
            Call Sleep(2000) '2000 Millisekunden Pause
End Sub
 
Upvote 0
Hi,

try the "sleep"-function:

Code:
Private Declare PtrSafe Sub Sleep Lib "kernel32.dll" ( _
    ByVal dwMilliseconds As Long)

Public Sub mySleep()
   'your code
            Call Sleep(2000) '2000 Millisekunden Pause
End Sub



only works once ?
 
Upvote 0
I would like a looped code that waits 2 seconds then refreshes the links e.g calculate then waits 2 seconds then refreshes - basically a loop

A Sleep loop would never leave VBA. Perhaps you want the following in a normal module (not a worksheet module):

Code:
Private nextTime As Date

Sub startit()
nextTime = Now + TimeSerial(0,0,2), "repeatit"
Application.OnTime nextTime, "repeatit"
end Sub

Sub repeatit()
' do whatever you need here to "refresh the links"; perhaps:
' Application.Calculate
' then...
nextTime = Now + TimeSerial(0,0,2), "repeatit"
Application.OnTime nextTime, "repeatit"
End Sub

Sub stoptit
On Error Resume Next
Application.OnTime nextTime, "repeatit",, False
End Sub

To avoid having to remember to execute "stopit" manually, you also might want to put the following into the ThisWorkbook module:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
stopit
End Sub
 
Last edited:
Upvote 0
A Sleep loop would never leave VBA. Perhaps you want the following in a normal module (not a worksheet module):

Code:
Private nextTime As Date

Sub startit()
nextTime = Now + TimeSerial(0,0,2), "repeatit"
Application.OnTime nextTime, "repeatit"
end Sub

Sub repeatit()
' do whatever you need here to "refresh the links"; perhaps:
' Application.Calculate
' then...
nextTime = Now + TimeSerial(0,0,2), "repeatit"
Application.OnTime nextTime, "repeatit"
End Sub

Sub stoptit
On Error Resume Next
Application.OnTime nextTime, "repeatit",, False
End Sub

To avoid having to remember to execute "stopit" manually, you also might want to put the following into the ThisWorkbook module:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
stopit
End Sub




hi still not working and in the vba editor these lines are red font

nextTime = Now + TimeSerial(0,0,2), "repeatit"
 
Upvote 0
I guess what I'm trying to achieve is this

Calculate
Application.Wait (Now + TimeValue("0:00:05"))
Calculate
Application.Wait (Now + TimeValue("0:00:05"))
Calculate
Application.Wait (Now + TimeValue("0:00:05"))
Calculate
Application.Wait (Now + TimeValue("0:00:05"))
Calculate
Application.Wait (Now + TimeValue("0:00:05"))
Calculate


but not needing so many lines hahahaha

also the ability to pause the code by clicking a shape then after so many seconds it reverts to the refresh again
 
Upvote 0
in the vba editor these lines are red font
nextTime = Now + TimeSerial(0,0,2), "repeatit"

An obvious last-minute copy-and-paste error. The lines should read:

nextTime = Now + TimeSerial(0,0,2)
 
Upvote 0
Calculate
Application.Wait (Now + TimeValue("0:00:05"))
Calculate
Application.Wait (Now + TimeValue("0:00:05"))
[....]
but not needing so many lines hahahaha

Again, like Sleep, that stays in VBA all of the time. It does not allow you to use Excel while waiting; and Excel is not executing in the background.

Use my Application.Ontime paradigm, simply changing TimeSerial(0,0,2) to TimeSerial(0,0,5) or whatever you finally settle on.

BTW, there is reason to use TimeValue("0:00:05") instead of TimeSerial(0,0,5). The latter is more efficient. It is also localization-independent; some regions do not use ":" as the time separator. But feel free to use TimeValue, if your enamored with it.

Also, if are content with Calculate then the 5-sec delay (not exactly what you wrong initially), you do not need my "startit" subroutine. Just use my "repeatit" subroutine.

also the ability to pause the code by clicking a shape then after so many seconds it reverts to the refresh again

Just another Application.Ontime subroutine; or use the "stopit" and "startit" subroutines that I provided.

As for assigning a "shape" to click on, click Developer > Insert > Form Controls > Button.

If you do not see the Developer tab in your Ribbon, right-click any tab in your Ribbon (e.g. File) and click Customize Ribbon.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

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