# Run every hour, for 12 hours.

#### cbzdmh

##### Board Regular
I'm trybing to get a macro to run every hour for a period of 12 hours but it just seems to run 12 times in a row.

Sub RunTwelveTimes()

'TimeSerial(hours, minutes, seconds)
For i = 1 To 12
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Next i

End Sub

### Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
The short answer is that you haven't referenced i anywhere... as you have it, this is the equivalent to

Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"
Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"

which will trigger "RunThisProc" twelve times 1 hr from now.

Would this work?

Sub RunTwelveTimes()

'TimeSerial(hours, minutes, seconds)
For i = 1 To 12
Application.OnTime Now + TimeSerial(i, 0, 0), "RunThisProc"
Next i

End Sub

That's getting to the long answer

There's probably a better way to do this, but this should work... you'd trigger this by calling the initialize macro, and it would run from there...

Code:
``````Option Explicit
Public i As Integer

Sub RunTwelveTimes()
If i = 12 Then Exit Sub
'TimeSerial(hours, minutes, seconds)

Application.OnTime Now + TimeSerial(1, 0, 0), "RunThisProc"

i = i + 1

End Sub

Sub runthisproc()
'do some stuff
Call RunTwelveTimes
End Sub

Sub initialize()
i = 0
RunTwelveTimes
End Sub``````

Replies
4
Views
425
Replies
3
Views
2K
Replies
3
Views
1K
Replies
5
Views
348
Replies
1
Views
759

1,196,325
Messages
6,014,653
Members
441,834
Latest member
GHOSTOF309

### 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.

### Which adblocker are you using?

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

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