Run every hour, for 12 hours.

cbzdmh

Board Regular
Joined
Jul 16, 2007
Messages
58
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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
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.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,184
Members
448,554
Latest member
Gleisner2

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