excel - Having problems with Macro running every 15 minutes correctly -
i have code (see below) suppose run code every 15 minutes data refreshed yahoo finance , recorded in appropriate cells. macro suppose run on own once run first time. run first time after 15 min start doing every 2 min 15 min. isn't consistent. below code picture of producing.
sub timestamp() ' ' timestamp macro ' ' keyboard shortcut: ctrl+shift+a ' ' following refreshes data application.calculatefullrebuild ' following inputs exchange date (l1) , time (n1) next available cell in column activesheet .cells(rows.count, "a").end(xlup).offset(1, 0) .value = application.evaluate("concatenate(l1,n1)") .wraptext = false end end ' following inputs current price of stock (g3) next available cell in column d activesheet .cells(rows.count, "d").end(xlup).offset(1, 0) = .range("g3").value2 end ' following inputs date of exchange (l1) next available cell in column b activesheet.cells(rows.count, "b").end(xlup).offset(1, 0) 'this sets value of b2 value of l1 .value = activesheet.range("l1").value2 .wraptext = false end ' following inputs time of exchange (n1) next available cell in column c activesheet.cells(rows.count, "c").end(xlup).offset(1, 0) 'this sets value of b2 value of l1 .value = activesheet.range("n1").value2 .wraptext = false end ' following runs timestamp macro every 15 minutes application.ontime + timevalue("00:15:00"), "timestamp" end sub 
a global variable means accessible routine or function in macro. declare variable global doing outside (above) rest of code.
chip has excellent tutorial should address issue.
declare public variables in standard code module, outside of , before procedure (sub or function) declaration:
public runwhen double public const crunintervalseconds = 120 ' 2 minutes public const crunwhat = "thesub" ' name of procedure run to start repeatable timer, create procedure named starttimer shown below:
sub starttimer() runwhen = + timeserial(0,0,crunintervalseconds) application.ontime earliesttime:=runwhen, procedure:=crunwhat, _ schedule:=true end sub this stores time run procedure in variable runwhen, 2 minutes after current time.
next, need write procedure called ontime. example,
sub thesub() '''''''''''''''''''''''' ' code here '''''''''''''''''''''''' starttimer ' reschedule procedure end sub this procedure executes whatever code include in it, , @ end calls starttimer procedure schedule ontime event. how periodic calls implemented. note if close workbook while ontime event pending, excel re-open workbook execute procedure , not close workbook after ontime event finished.
Comments
Post a Comment