How to stop VBA code running?

后端 未结 7 832
臣服心动
臣服心动 2020-12-08 11:22

Say I have a button embedded into my spreadsheet that launches some VBA function.

Private Sub CommandButton1_Click()
    SomeVBASub
End Sub

Private Sub Some         


        
7条回答
  •  旧时难觅i
    2020-12-08 12:02

    Or, if you want to avoid the use of a global variable you could use the rarely used .Tag property of the userform:

    Private Sub CommandButton1_Click()
        Me.CommandButton1.Enabled = False 'Disabling button so user cannot push it
                                          'multiple times
        Me.CommandButton1.caption = "Wait..." 'Jamie's suggestion
        Me.Tag = "Cancel"
    End Sub
    
    Private Sub SomeVBASub
        If LCase(UserForm1.Tag) = "cancel" Then
            GoTo StopProcess
        Else
            'DoStuff
        End If
    
    Exit Sub
    StopProcess:
        'Here you can do some steps to be able to cancel process adequately
        'i.e. setting collections to "Nothing" deleting some files...
    End Sub
    

提交回复
热议问题