PDA

View Full Version : Is it possible to run code every 24th hour for 3 days?



JKB
08-10-2014, 02:15 AM
Hi out there!


Does anybody know whether it is possible to run a bunch of code, at a certain time, 3 times, for fx three days?
What i want is to run some code, which collects stock prices from yahoo finance. The idea is that i should be able to click and activate the code, and then at fx 18:00 pm each day for 3 days i want it to download the stock-prices down into my spreadsheet in accordingly 3 columns. Does anybody know whether this is possible/how?


Hope someone can help me!


Cheers, have a great day
JKB

JKB
08-10-2014, 02:56 AM
I know i can use the Application.OnTime method, but my problem is doing it as a loop! So that it changes column for each day it runs (Using a function with the current price as output)

westconn1
08-10-2014, 03:30 AM
but my problem is doing it as a loop!call application.ontime within the called procedure to run again, based on whatever criteria

JKB
08-10-2014, 03:35 AM
But there doesnt exist a counter, for the function which sets an integer = the number of times the procedure has been running? Or can i make one? My problem is that im fairly new, so i dont understand how to do what you are suggesting! :)

westconn1
08-10-2014, 03:51 AM
Or can i make one?static variable, or a count of days from when first run


sub test
static cnt as integer
' some code here
cnt = cnt +1
if cnt < 3 then Application.OnTime Now + 1, "test"
end subthis should run the code 24 hours from when called, if you want more accurate timing you would have to use something like date +1 +timeserial(18, 0, 0)

p45cal
08-10-2014, 04:42 AM
Do you really need a counter?
Perhaps a separate bit of code which schedules all 3 instances at once; three lines along the lines of westconn' suggestion
Application.ontime date+1+timeserial("18:00:00") , "test"
Application.ontime date+2+timeserial("18:00:00") , "test"
Application.ontime date+3+timeserial("18:00:00") , "test"
the called procedure could either carry an argument to identify the destination column or the procedure could add a header in the column which the code will see with the likes of cells(1, columns.count).end(xltoleft).column+1