我有一个宏,我试图在第二天的晚上6点到230点之间每30分钟运行一次。
我尝试使用Workbook.Open和Application.OnTime来完成此操作,但是我意识到它看起来只会将所有内容安排1次。第二天,什么都没跑。我在ThisWorkbook VBA页面中有此代码。
如何修改它,使其在那一小时连续运行而不必关闭并重新打开工作表
Private Sub Workbook_Open()
Application.OnTime TimeValue("18:00:00"), "PopulateData"
Application.OnTime TimeValue("18:30:00"), "PopulateData"
Application.OnTime TimeValue("19:00:00"), "PopulateData"
Application.OnTime TimeValue("19:30:00"), "PopulateData"
Application.OnTime TimeValue("20:00:00"), "PopulateData"
Application.OnTime TimeValue("20:30:00"), "PopulateData"
Application.OnTime TimeValue("21:00:00"), "PopulateData"
Application.OnTime TimeValue("21:30:00"), "PopulateData"
Application.OnTime TimeValue("22:00:00"), "PopulateData"
Application.OnTime TimeValue("23:30:00"), "PopulateData"
Application.OnTime TimeValue("00:00:00"), "PopulateData"
Application.OnTime TimeValue("00:30:00"), "PopulateData"
Application.OnTime TimeValue("01:00:00"), "PopulateData"
Application.OnTime TimeValue("01:30:00"), "PopulateData"
Application.OnTime TimeValue("02:00:00"), "PopulateData"
Application.OnTime TimeValue("02:30:00"), "PopulateData"
Application.OnTime TimeValue("03:00:00"), "PopulateData"
Application.OnTime TimeValue("03:30:00"), "PopulateData"
Application.OnTime TimeValue("04:00:00"), "PopulateData"
Application.OnTime TimeValue("04:30:00"), "PopulateData"
Application.OnTime TimeValue("05:00:00"), "PopulateData"
Application.OnTime TimeValue("05:30:00"), "PopulateData"
Application.OnTime TimeValue("06:00:00"), "PopulateData"
Application.OnTime TimeValue("06:30:00"), "PopulateData"
Application.OnTime TimeValue("07:00:00"), "PopulateData"
Application.OnTime TimeValue("07:30:00"), "PopulateData"
Application.OnTime TimeValue("08:00:00"), "PopulateData"
Application.OnTime TimeValue("08:30:00"), "PopulateData"
Application.OnTime TimeValue("09:00:00"), "PopulateData"
Application.OnTime TimeValue("09:30:00"), "PopulateData"
Application.OnTime TimeValue("10:00:00"), "PopulateData"
Application.OnTime TimeValue("10:30:00"), "PopulateData"
Application.OnTime TimeValue("11:00:00"), "PopulateData"
Application.OnTime TimeValue("11:30:00"), "PopulateData"
Application.OnTime TimeValue("12:00:00"), "PopulateData"
Application.OnTime TimeValue("12:30:00"), "PopulateData"
Application.OnTime TimeValue("13:00:00"), "PopulateData"
Application.OnTime TimeValue("13:30:00"), "PopulateData"
Application.OnTime TimeValue("14:00:00"), "PopulateData"
Application.OnTime TimeValue("14:30:00"), "PopulateData"
End Sub
答案 0 :(得分:1)
您可以使用以下方法简化操作:
将以下内容放入您的ThisWorkbook
Private Sub Workbook_Open()
Dim t As Long
t = 24 / (30 / 60)
Application.OnTime TimeValue(Format(WorksheetFunction.Ceiling(Now * t, 1) / t, "HH:mm:ss")), "UpdateCaller"
End Sub
调用
Public Sub UpdateCaller()
Dim tme As Date
Dim t As Long
If TimeValue(Format(Now, "HH:mm:ss")) <= TimeValue("14:30:00") Or TimeValue(Format(Now, "HH:mm:ss")) >= TimeValue("18:00:00") Then
Call PopulateData
End If
t = 24 / (30 / 60)
tme = TimeValue(Format(WorksheetFunction.Ceiling((Now + TimeValue("00:00:05")) * t, 1) / t, "HH:mm:ss"))
Debug.Print "Procedure ""UpdateCaller"" will next run at: ", tme
Application.OnTime tme, "UpdateCaller"
End Sub
您将需要保持工作簿处于打开状态。如果工作簿已关闭,则可以使用Windows Task Scheduler每30分钟打开一次。