re loop for next

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

re loop for next

Post by sal21 »

I have a for next loop....
I need to re loop The for next at every 5 minutes... how to ? But
only when the for next is finished.
Example:

For i = 0 to 10

Next i

Here stop for 5 minutes
Re loop the for next
Ecc....

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: re loop for next

Post by HansV »

If it's in Excel:

Code: Select all

Dim dtmSchedule as Date

Sub StartLooping()
    Dim i As Long
    For i = 0 To 10
        ...
    Next i
    dtmSchedule = Now + TimeSerial(0, 5, 0)
    Application.OnTime dtmSchedule, "StartLooping"
End Sub

Sub StopLooping()
    Application.OnTime dtmSchedule, "StartLooping", , False
End Sub
Run StartLooping when you want to start, and run StopLooping when you want to stop.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: re loop for next

Post by sal21 »

HansV wrote:If it's in Excel:

Code: Select all

Dim dtmSchedule as Date

Sub StartLooping()
    Dim i As Long
    For i = 0 To 10
        ...
    Next i
    dtmSchedule = Now + TimeSerial(0, 5, 0)
    Application.OnTime dtmSchedule, "StartLooping"
End Sub

Sub StopLooping()
    Application.OnTime dtmSchedule, "StartLooping", , False
End Sub
Run StartLooping when you want to start, and run StopLooping when you want to stop.
ops...

but really i need in vba for Excel.
sorry me

errror in:
Application.OnTime dtmSchedule, "StartLooping", , False
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: re loop for next

Post by HansV »

The code runs in Excel, I tested it.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: re loop for next

Post by Rudi »

You can only run StopLooping while StartLooping is running.
If you run StopLooping on its own it will debug as there is nothing to stop.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: re loop for next

Post by sal21 »

HansV wrote:The code runs in Excel, I tested it.

Resolved....

but how to stop the timer routine and exit sub if the cirrent tme is => 16:30?

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: re loop for next

Post by Rudi »

Try this...
Note: Its air code...untested!

Code: Select all

Option Explicit
Dim dtmSchedule As Date

Sub StartLooping()
   Dim i As Long

   For i = 0 To 10
      '...
   Next i

   If Now >= TimeValue("16:30:00 PM") Then
      StopLooping
   Else
      dtmSchedule = Now + TimeSerial(0, 5, 0)
      Application.OnTime dtmSchedule, "StartLooping"
   End If

End Sub

Sub StopLooping()
   On Error Resume Next
   Application.OnTime dtmSchedule, "StartLooping", , False
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.