暂停 VBA 循环以允许使用或不使用用户表单编辑工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21431154/
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
Pausing a VBA loop to allow editing of worksheet, with or without userform
提问by Anthony Bird
I have a procedure that consists of several do
and for
loops and i would like to find an easy way to 'pause' the routine and allow the user to edit the sheet, with a msgbox or userform to resume execution where it left off.
我有一个由几个do
和for
循环组成的过程,我想找到一种简单的方法来“暂停”例程并允许用户编辑工作表,使用 msgbox 或用户表单从它停止的地方恢复执行。
I would like to do something like this
我想做这样的事情
dim pause as boolean
pause=false
For i = 1 To 40
Worksheets("sheet1").Range("A" & i) = i
If i = 20 Then
UserForm1.Show vbmodeless
Pause = true
Do until pause = false
loop
Else
End If
Next i
End Sub
Where the pause condition would be set by a sub on the userform. This do loop just crashes.
暂停条件将由用户表单上的子设置。这个 do 循环只是崩溃了。
Ideallyi would like the userform to have buttons that can run subs but also allow direct editing of cells while execution is paused.
理想情况下,我希望用户窗体具有可以运行 subs 但也允许在暂停执行时直接编辑单元格的按钮。
回答by Gary's Student
Here is a typical control structure that allows the user to perform some actions in the middle of a macro. When the user is done, they run OKToContinueto allow the macro to continue with the second part:
这是一个典型的控制结构,它允许用户在宏中间执行一些操作。用户完成后,他们运行OKToContinue以允许宏继续第二部分:
Dim AllowedToContinue As Boolean
Sub FirstPartSecondPart()
AllowedToContinue = False
MsgBox "allow user to perform actions"
Do Until AllowedToContinue
DoEvents
Loop
MsgBox "doing second part"
End Sub
Sub OKToContinuw()
AllowedToContinue = True
End Sub