excel application.ontime每小时运行一次,无论计时器何时启动 [英] excel application.ontime to run on the hour every hour regardless of when the timer is started

查看:37
本文介绍了excel application.ontime每小时运行一次,无论计时器何时启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VBA子,需要每隔一小时运行一次.因此,在7:00、8:00等...到目前为止,我可以让它每60分钟运行一次,但为了使其在小时内运行,操作员必须在该小时上完全启动计时器.我为此使用的代码如下:

I have a VBA sub that need to run at the change of every hour. so at 7:00, 8:00, etc... As of right now I can get it to run every 60 min but in order for it to run on the hour the operator must start the timer exactly on the hour. my code that im using for this is as follows:

Public RunWhen As Double
Public Const cRunIntervalSeconds = 3600
Public Const cRunWhat = "transfer"
-----------------------------------------------------------------------
Sub StartTimer()
    RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
        Schedule:=True

End Sub

我一直试图找到一种方法来使它在小时内无法成功运行,现在,我所看到的每个地方都只是告诉我做我已经做过的事情,并确保将其完全启动小时.

I have been trying to find a way to get this to run exactly on the hour unsuccessfully for a while now, every place I look just tells me to do exactly what i have already done and just make sure i start it exactly on the hour.

例如:如果操作员直到7:10才到达车站并按下运行代码的按钮,我仍然希望它在8:00、9:00和9:00执行转移"子程序.很快.

for example: If the operator doesn't get to the station until 7:10 and hits the button that runs the code, I still want it to execute the sub called "transfer" at 8:00 then 9:00 and so on.

您如何建议我修改或添加到此代码中,以完成我想做的事情?谢谢您的帮助!

how would you suggest I modify or add to this code to get it to accomplish what I am trying to do? thank you for the help!

推荐答案

根据要求,以我最喜欢的公式作为答案:

as requested, my favorite formula as answer:

RunWhen = Int(24 * Now + 1) / 24

因为:它很短,应该100%不解自明:D

because: it is short and should be 100% self-explaining :D

这篇关于excel application.ontime每小时运行一次,无论计时器何时启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆