vba DoEvents、等待和编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16073972/
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
DoEvents, Waiting, and Editing
提问by user2263642
I have a set of code that contains:
我有一组代码,其中包含:
Application.Wait (Now + TimeValue("4:00:00"))
This is essentially pausing the macro for a four hour window from 3 AM (when it finishs running the code) till 7 AM (when it should resume). The code is on an endless loop essentially.
这实质上是将宏从凌晨 3 点(完成代码运行时)到早上 7 点(应该恢复时)暂停了四个小时的窗口。代码本质上处于无限循环中。
I want the user to be able to have control during that time to edit certain cells. I have tried
我希望用户能够在这段时间内控制编辑某些单元格。我试过了
DoEvents
but have not found the way to keep the macro running, yet provide control to the user during that time when the macro is doing nothing but waiting.
但还没有找到保持宏运行的方法,但在宏除了等待之外什么都不做的时候为用户提供控制。
Any insight would be appreciated. Thanks!
任何见解将不胜感激。谢谢!
EDIT:
编辑:
One more followup question. I created this macro to reference the actual macro "Production_Board". I want this macro to run all the time and refresh as often as possible. By using the goto startagain, it tries to start to launch the macro again before the macro has even started due to the "ontime" delay interval.
还有一个后续问题。我创建了这个宏来引用实际的宏“Production_Board”。我希望这个宏一直运行并尽可能频繁地刷新。通过使用 goto startagain,由于“ontime”延迟间隔,它甚至会在宏启动之前尝试再次启动宏。
How could I make the sub RunMacro start again the second that the macro "Production_Board" finishes?
我怎样才能让子 RunMacro 在宏“Production_Board”完成的第二个开始?
Sub RunMacro
startagain:
Dim hour As Integer
Dim OT As String
hour = 0
OT = "Empty"
hour = Sheets("Calculations").Range("DR1").Value
OT = Sheets("Black").Range("D4").Value
If OT = "Y" Then
If hour = 3 Or hour = 4 Then
Application.OnTime TimeValue("05:00:00"), "Aespire_Production_Board"
Else
Application.OnTime Now + TimeValue("00:00:30"), "Aespire_Production_Board"
End If
Else
If hour = 3 Or hour = 4 Or hour = 5 Or hour = 6 Then
Application.OnTime TimeValue("07:00:00"), "Aespire_Production_Board"
Else
Application.OnTime Now + TimeValue("00:00:30"), "Aespire_Production_Board"
End If
DoEvents
GoTo startagain
回答by Doug Glancy
Instead of Wait
, try OnTime
. To demonstrate, paste this in a normal module and run Test
. Range A1 of the active sheet will increment every five seconds and you'll be able to work in between. It also works if you are in Edit mode when the five seconds elapses:
而不是Wait
,尝试OnTime
。为了演示,将其粘贴到普通模块中并运行Test
. 活动工作表的范围 A1 将每五秒增加一次,您将能够在两者之间工作。如果五秒钟过去后您处于编辑模式,它也可以工作:
Sub test()
test2
End Sub
Sub test2()
ActiveSheet.Cells(1, 1).Value = ActiveSheet.Cells(1, 1).Value + 1
Application.OnTime Now + TimeValue("00:00:5"), "test2"
End Sub
Note that the OnTime
statement at the end of the sub calls the sub again recursively. Here'ssome more info.
请注意,OnTime
sub 末尾的语句再次递归调用 sub。这里有更多信息。
回答by KacireeSoftware
Sub mySub()
Do
If Time() >= #3:00:00 AM# And Time() <= #7:00:00 AM# Then
Aespire_Production_Board
End If
DoEvents
Loop
End Sub
Once you start mySub() it will run indefinately. Between 3 AM and 7 AM it will run Aespire_Production_Board on every loop. It also allows the user to interact. The code can be put into Break mode using CTRL-Break.
一旦您启动 mySub() 它将无限期地运行。在凌晨 3 点到 7 点之间,它将在每个循环中运行 Aespire_Production_Board。它还允许用户进行交互。可以使用 CTRL-Break 将代码置于 Break 模式。