How to stop VBA code running?

后端 未结 7 836
臣服心动
臣服心动 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条回答
  •  没有蜡笔的小新
    2020-12-08 12:14

    Add another button called "CancelButton" that sets a flag, and then check for that flag.

    If you have long loops in the "stuff" then check for it there too and exit if it's set. Use DoEvents inside long loops to ensure that the UI works.

    Bool Cancel
    Private Sub CancelButton_OnClick()
        Cancel=True
    End Sub
    ...
    Private Sub SomeVBASub
        Cancel=False
        DoStuff
        If Cancel Then Exit Sub
        DoAnotherStuff
        If Cancel Then Exit Sub
        AndFinallyDothis
    End Sub
    

提交回复
热议问题