Running macros at set times

asjmoron

Board Regular
Joined
Apr 26, 2016
Messages
98
Office Version
  1. 2016
Platform
  1. Windows
Morning,

I have the below code to fire a macro at set times in the day to coincide with a datalink that refreshes just before. However, I have to close and re-open the workbook every morning to start the sequence again. Is there a way to keep these times on a loop to ensure that I can leave the book open indefinately and the macros still fire?

VBA Code:
Private Sub Workbook_Open()


    Application.OnTime TimeValue("09:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("10:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("11:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("12:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("13:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("14:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("15:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("16:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("17:05:05"), "OpenLatestFile"

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi there. You could just wrap around like this:
VBA Code:
Private Sub Workbook_Open()

looper:

    Application.OnTime TimeValue("09:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("10:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("11:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("12:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("13:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("14:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("15:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("16:05:05"), "OpenLatestFile"
    Application.OnTime TimeValue("17:05:05"), "OpenLatestFile"
GoTo looper
End Sub
 
Upvote 0
Thanks @jmacleary I'll give that a go.

I did try using

DO
my code
LOOP

But im not sure if that would work?
 
Upvote 0
Hey. I just tested both options but all it seems to is try to run the whole process immediatley. I need it to still run incrementally at the times then start again
 
Upvote 0
The way to do it is to use a scheduler which call itself at appropriate time something like this ( untested so I have probably got some syntax wrong)
VBA Code:
Sub nextcall()
Call OpenLatestFile
timenow = Now()
If timenow > TimeSerial(17, 0, 0) Then
runwhen = Now + TimeSerial(14, 0, 0)
  Else
runwhen = Now + TimeSerial(1, 0, 0)
End If
Application.OnTime TimeValue(runwhen, "nextcall")
End Sub
 
Upvote 0
yes ofthelip is right- I was being dim.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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