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
 
still not working and in the vba editor these lines are red font
nextTime = Now + TimeSerial(0,0,2), "repeatit"

FYI, finally got around to testing. Works as __I__ intend it with the correction of the obvious copy-and-paste error above. Whether it works as you intend it might be a different story.

Here's the test code. Red lines are part of the test; you would not include them. It's a little tricky to execute "stopit" within 2 seconds of clicking OK for the Msgbox. I do it in two steps.

Rich (BB code):
Private nextTime As Date
Private lastTime As Date

Sub startit()
MsgBox Now & "    startit"
lastTime = Now
nextTime = Now + TimeSerial(0, 0, 2)
Application.OnTime nextTime, "repeatit"
End Sub

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

Sub stoptit()
On Error Resume Next
MsgBox lastTime & vbNewLine & Now & "    stopit"
Application.OnTime nextTime, "repeatit", , False
End Sub
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi,

is the problem to build a loop?

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

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

The application.wait should work as well.

regards
 
Upvote 0
Hi,

is the problem to build a loop?

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

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

The application.wait should work as well.

regards

Hi fennek - thanks this seems to work as I see the calculate work - I start the code by a shortcut ctrl+z

the way I can exit the code is pressing escape then end !
 
Upvote 0
Hi fennek - thanks this seems to work as I see the calculate work - I start the code by a shortcut ctrl+z

the way I can exit the code is pressing escape then end !


hi is their an alternative that does the above however only when not using - so pauses for you to edit some cells then if not using excel executes the code again until next edit?
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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