如何在VBA内加入定时器
展开全部
VBA程序运行中,你可能需要自动执行某些代码或者定时执行某些计划任务,如自动保存,这就要使用定时器来完成。这里水文工具集给出VBA中使用计时器的两种方法,其一是使用Application对象的OnTime方法来运行任务,其二是通过Windows API函数来完成,新手用第一种方式比较容易,具体实例如下:
一、采用Application.OnTime实现计时器的方式
view sourceprint?
01.'================================
02.' VBA采用Application.OnTime实现计时器
03.'
04.' http://www.cnhup.com
05.'================================
06.Public RunWhen As Double
07.Public Const cRunIntervalSeconds = 120 ' two minutes
08.Public Const cRunWhat = "TheSub" ' the name of the procedure to run
09.Sub StartTimer()
10.RunWhen = Now + TimeSerial(0,0,cRunIntervalSeconds)
11.Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
12.Schedule:=True
13.End Sub
14.Sub TheSub()
15.StartTimer ' Reschedule the procedure
16.End Sub
17.Sub StopTimer()
18.On Error Resume Next
19.Application.OnTime EarliestTime:=RunWhen,Procedure:=cRunWhat, _
20.Schedule:=False
21.End Sub
一、采用Application.OnTime实现计时器的方式
view sourceprint?
01.'================================
02.' VBA采用Application.OnTime实现计时器
03.'
04.' http://www.cnhup.com
05.'================================
06.Public RunWhen As Double
07.Public Const cRunIntervalSeconds = 120 ' two minutes
08.Public Const cRunWhat = "TheSub" ' the name of the procedure to run
09.Sub StartTimer()
10.RunWhen = Now + TimeSerial(0,0,cRunIntervalSeconds)
11.Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
12.Schedule:=True
13.End Sub
14.Sub TheSub()
15.StartTimer ' Reschedule the procedure
16.End Sub
17.Sub StopTimer()
18.On Error Resume Next
19.Application.OnTime EarliestTime:=RunWhen,Procedure:=cRunWhat, _
20.Schedule:=False
21.End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试试Application.OnTime
追问
请写详细一点呀
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询