How to use Application.OnTime to call a macro at a set time everyday, without having to close workbook

心已入冬 提交于 2019-11-28 05:13:40

问题


I have written a macro that uses Application.OnTime that works if I manually execute the macro. I'm trying to automate this process so I don't have to write Application.OnTime in "This Workbook" or (Private Sub Workbook_Open() Most of you do this because you can have windows scheduler open the workbook at a certain time which starts the macros on open. I CANNOT USE SCHEDULER.

Because I am not able to use windows scheduler I will keep the workbook open and the timer should refresh my data then Call "my Macro" at a certain time everyday.

Where do I place this code, and how do I set an auto timer?


回答1:


You could create a kind of recurrence procedure. It could look as follows:

Sub Call_At_3_30()
    'first call appropriate procedure 
    Call myProcedure
    'next, set new calling time
    Application.OnTime TimeValue("3:30:00"), "Call_At_3_30"
End Sub

Somewhere you will keep your main procedure, it this situation:

Sub myProcedure
    'your code here
End Sub

In this situation you need to run first subroutine Call_At_3_30 only once. But you need to remember that Excel must be turned on all the time.

Optionally, if you want to call your procedure after 24 hours you could change .OnTime instruction in this way:

Application.OnTime Now + 1, "Call_At_3_30"

Some other modifications are possible, too.



来源:https://stackoverflow.com/questions/17301512/how-to-use-application-ontime-to-call-a-macro-at-a-set-time-everyday-without-ha

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!