macros在特定的时间开始
我发现有消息说要使用
Application.OnTime TimeValue("18:00:00"), "MyMacro"
但我似乎无法得到它的工作。 这就是我所input的内容
Sub TimeStamp() ' ' TimeStamp Macro ' ' Keyboard Shortcut: Ctrl+Shift+T ' Application.OnTime TimeValue("13:25:00"), "TimeStamp" ' Following refreshes the data Application.CalculateFullRebuild
然后是End Sub
的其余部分
无论出于何种原因,当我启动macros时,都不会等到指定的时间。 我打破了什么语法规则?
macros被触发时触发macros。 OnTime安排它自动运行,但为什么应该阻止你手动运行它? 把它想象成一个病毒扫描。 许多人将自己的电脑configuration为在特定的时间进行自动扫描,但是可以随时运行非计划扫描。 如果你想确保你的macros在一定的时间之前没有任何效果,那么使用一个包含Now的if – then语句(如果它太早退出sub) – 虽然根本不运行它当你不想要。
我多年来成功使用的另一种方法是创build一个计划调用macros的小型vb脚本。 我对另一个非常类似的问题的答案显示了这个示例脚本
Dim xlApp Dim xlWkb Set xlApp = CreateObject("excel.application") Set xlWkb = xlApp.Workbooks.Open("PATH TO YOUR FILE") xlApp.Visible = True xlWkb.RunAutoMacros 1 'enables macros to be run on open xlApp.Run ("YOUR PROCEDURE") xlApp.Workbooks("YOUR WORKBOOK NAME").Save 'Save the workbook xlApp.Quit 'quits excel
通过任务计划程序进行安排。 我的其他答案可以在这里find
这是我最后感谢你们几个人的帮助。 我用了:
Private Sub Workbook_Open() Application.Wait "6:45:00" Call TimeStamp End Sub
并把它放在“ThisWorkbook”中。 这使得当我打开我的工作簿时,macros自动启动,但是等到下午3点03分执行其余的任务。
接下来,我需要每15分钟刷新一次,所以我使用了User:Findwindowbuild议的Chip解决scheme。 代码如下
Public RunWhen As Double Public Const cRunIntervalSeconds = 900 ' 15 minutes Public Const cRunWhat = "TimeStamp" ' the name of the procedure to run Sub StartTimer() If Time < TimeSerial(13, 15, 0) Then RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds) Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _ Schedule:=True End If End Sub
在TimeStampmacros的末尾,我放置了Call StartTimer
以便每次macros运行时,计时器都会从当前时间计划一个新的运行时间15分钟。 If Time < TimeSerial(13, 15, 0) Then
允许macros在下午1:15停止运行,我希望它停止。