我想让用户做出选择,运行一些代码,暂停另一个选择,运行更多代码?
我处理包含大量表格的文档,这些表格最终会转换为 HTML。有时,两个类似表上的格式转换不一样。了解转换器的工作原理后,我想从一个表中复制所有格式数据并将其“粘贴”到另一个表中。
我有一个用户表单的想法,让用户选择一些东西,点击复制按钮,选择其他东西并点击粘贴按钮。
计时器功能允许您执行此操作。它可能不是最好的编码方式,但它是您问题的答案:
'1st code here
Start = Timer
Do While Timer < Start + 10
DoEvents
Loop
'do 2nd code here
DoEvents
允许用户选择文本等。 10 秒后,代码在“第二个代码”处恢复。
您可以使用全局全局变量:
Public myVar as Variant
Sub fillmyVar()
myVar = Selection
End Sub
Sub doSth()
' use myVar and the new selected text
End Sub
使用 Aaron 的答案并将其与用户窗体中的 ToggleButton 结合,您可以成功暂停代码。有了这个,您就可以在其他选择中工作以更改操作。
我最初没有使用全局变量或公共变量,但很快就知道在Subs
和之间传递数据更容易Userforms
用户表单:
Public Sub ToggleButton1_AfterUpdate()
'Program is Paused / Selected to Pause
If ProgBar.ToggleButton1.Value = True Then
'Changing the Text of the Toggle button once the program is selected to Pause
'If program paused then button will display continue
ProgBar.ToggleButton1.Caption = "Continue"
'For Sending to Loop Code
ProgramStatus = "0"
Call LoopCode.PrgStat(ProgramStatus)
End If
'Program is running / Selected to run
If ProgBar.ToggleButton1.Value = False Then
'Changing the Text of the Toggle button once the program is selected to continue
'If program running then button will display pause
ProgBar.ToggleButton1.Caption = "Pause"
'For Sending to Loop Code
ProgramStatus = "1"
Call LoopCode.PrgStat(ProgramStatus)
End If
End Sub
在您的模块中
Public Status As String
Public Sub PrgStat(ByVal ProgStatus As String)
Status = ProgStatus
End Sub
Sub SomeSub()
Do While
' Some Loop Code Running
If Status = "1" Then
'Toggle Not Pressed
End If
If Status = "0" Then
'Toggle Button Pressed
Do While Status = "0"
'Program will stop here until the togglebutton on the
'userform is pressed again which changes Status = 1
'This is where you can make another selection on the
'userform
DoEvents
Loop
End If
Loop
End Sub