Excel VBA Application.OnTime. I think its a bad idea to use this… thoughts either way?

后端 未结 2 596
醉话见心
醉话见心 2021-01-14 06:26

I have a number of users I support that are asking for things to happen automatically ( well more automagically but that\'s another point!).

One want events to happe

相关标签:
2条回答
  • 2021-01-14 06:47

    Application.OnTime is absolutely 100% reliable and is most definitely not dangerous. However, it is only exposed via VBA and you are regarding VBA as a "no no" for some reason here, so this option would appear to be unavailable to you.

    I would generally not use OnTime for long-term scheduling, such as scheduling Excel to execute a command each day at 5pm. The problem is that if the user closes Excel, then the OnTime scheduling is lost. What you would need, in this case, is to use the Task Scheduler, or create your own application or windows service to open Excel and execute your commands.

    For scheduling an event to occur every 120 seconds, however, using Application.OnTime would be perfect for this -- you would simply need to re-schedule OnTime to occur again in 120 seconds each time that OnTime calls back, because OnTime only fires once per scheduling, not on a repeat basis. I would absolutely use VBA for this task. If you don't want VBA commencing the action, that is fine: just have the VBA contained in a workbook which is then opened by your program or via the Task Scheduler. From that point onward, the VBA code can fire every 120 seconds.

    Make sense?

    0 讨论(0)
  • 2021-01-14 06:53

    You're right. an "infinite" interval of "onTime" calling itself, creates an infinite recursion.

    It will cause a stack overflow after few thousand/million/billion function calls, and it will "leak" memory.

    0 讨论(0)
提交回复
热议问题