vba 如何让vba每10分钟执行一次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22772898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:03:51  来源:igfitidea点击:

How to have vba execute every 10 minutes?

excelvbaexcel-vba

提问by Buras

I need to have my macro executed every 10 minutes .

我需要每 10 分钟执行一次我的宏。

This allows it to work in 10 minutes

这允许它在 10 分钟内工作

sub my_Procedure () 
msgbox "hello world"
end sub

sub test()
Application.OnTime Now + TimeValue("00:00:10"), "my_Procedure"
end sub

But this works only once . How can I have my macro execute every 10 minutes ?

但这只能工作一次。如何让我的宏每 10 分钟执行一次?

回答by Dmitry Pavliv

You should use this pattern:

你应该使用这个模式:

Sub my_Procedure()
    MsgBox "hello world"        
    Call test ' for starting timer again
End Sub

Sub test()
    Application.OnTime Now + TimeValue("00:10:00"), "my_Procedure"
End Sub

回答by Gary's Student

Consider:

考虑:

Public RunWhen As Double
Public Const cRunWhat = "my_Procedure"

Sub StartTimer()
RunWhen = Now + TimeSerial(0, 10, 0)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
     schedule:=True
End Sub

Sub StopTimer()
   On Error Resume Next
   Application.OnTime earliesttime:=RunWhen, _
       procedure:=cRunWhat, schedule:=False
End Sub

Sub my_Procedure()
    MsgBox "hello world"
    Call StartTimer
End Sub

all in a standard module..............be sure to run StopTimer before exiting Excel

全部在一个标准模块中......在退出 Excel 之前一定要运行 StopTimer

NOTE

笔记

The "minute" argument in TimeSerial is the secondargument.

TimeSerial 中的“分钟”参数是第二个参数。