Application.OnTime 方法 (Excel)Application.OnTime method (Excel)

安排一个过程在将来的特定时间运行(既可以是具体指定的某个时间,也可以是指定的一段时间之后)。Schedules a procedure to be run at a specified time in the future (either at a specific time of day or after a specific amount of time has passed).

语法Syntax

表达式OnTime(EarliestTimeProcedureLatestTimeSchedule)expression.OnTime (EarliestTime, Procedure, LatestTime, Schedule)

expression:表示 Application 对象的变量。expression A variable that represents an Application object.

参数Parameters

名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
EarliestTimeEarliestTime 必需Required VariantVariant 希望此过程运行的时间。The time when you want this procedure to be run.
ProcedureProcedure 必需Required StringString 要运行的过程名。The name of the procedure to be run.
LatestTimeLatestTime 可选Optional VariantVariant 可以运行相应过程的最后时间。The latest time at which the procedure can be run. 例如, 如果将_LatestTime_设置为_EarliestTime_ + 30, 并且 Microsoft Excel 在_EarliestTime_上未准备就绪、复制、剪切或查找模式, 因为另一个过程正在运行, 所以 Excel 将在第一个过程中等待30秒以完成第一个过程。For example, if LatestTime is set to EarliestTime + 30 and Microsoft Excel is not in Ready, Copy, Cut, or Find mode at EarliestTime because another procedure is running, Excel will wait 30 seconds for the first procedure to complete. 如果 Excel 在30秒内未处于就绪模式, 则不会运行该过程。If Excel is not in Ready mode within 30 seconds, the procedure won't be run. 如果省略此参数, Excel 将一直等待, 直到可以运行该过程。If this argument is omitted, Excel will wait until the procedure can be run.
ScheduleSchedule 可选Optional VariantVariant 如果为 True,则安排新的 OnTime 过程。True to schedule a new OnTime procedure. 如果为 False,则清除以前设置的过程。False to clear a previously set procedure. 默认值为 TrueThe default value is True.

备注Remarks

使用Now + TimeValue(time)可安排在一段特定时间 (从现在开始计数) 开始运行的操作。Use Now + TimeValue(time) to schedule something to be run when a specific amount of time (counting from now) has elapsed. 使用 TimeValue(time) 可安排某个过程只运行指定的时间。Use TimeValue(time) to schedule something to be run a specific time.

将_EarliestTime_的值四舍五入为最接近的秒数。The value of EarliestTime is rounded to the closest second.

将_Schedule_设置为False , 以清除以前使用相同的_过程_和_EarliestTime_值设置的过程。Set Schedule to False to clear a procedure previously set with the same Procedure and EarliestTime values.

Procedure 必须不带参数,并且不能在自定义类或表单中声明。Procedure must take no arguments and cannot be declared in a custom class or form.

示例Example

本示例从现在开始运行_我_的过程_15 秒。This example runs my_Procedure 15 seconds from now.

Application.OnTime Now + TimeValue("00:00:15"), "my_Procedure"

本示例将在下午5点运行_我_的过程_。This example runs my_Procedure at 5 P.M.

Application.OnTime TimeValue("17:00:00"), "my_Procedure"

本示例撤消前一个示例对 OnTime 的设置。This example cancels the OnTime setting from the previous example.

Application.OnTime EarliestTime:=TimeValue("17:00:00"), _ 
 Procedure:="my_Procedure", Schedule:=False

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.