暂停VBA代码

时间:2015-09-16 14:39:49

标签: vba word-vba

如何暂停代码运行,然后再让它继续运行。

1 个答案:

答案 0 :(得分:0)

在Userform中使用ToggleButton(启动您的代码并Modal = True

您可以成功暂停代码。有了这个,您可以使用其他选项通过addiotn按钮更改代码的操作或功能。

您需要使用公共/全局变量。

评论欢迎。

用户窗体:

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