如何停止VBA代码运行? [英] How to stop VBA code running?
问题描述
Private Sub CommandButton1_Click()
SomeVBASub
End Sub
Private Sub SomeVBASub
DoStuff
DoAnotherStuff
AndFinallyDothis
End Sub
我想有机会有一些取消按钮,将停止 SomeVBASub
执行在任意的时刻,我没有涉及 Ctrl + Break
在这里,因为我想静静地做。
我想这应该是很常见的问题,任何想法?
谢谢。
添加另一个名为CancelButton的按钮,设置标志,然后检查该标志。
如果您在东西中有长的循环,那么在那里检查它,如果已设置,则退出。在长循环内使用DoEvents来确保UI可以正常工作。
Bool取消
私有子取消Button_OnClick()
取消= True
End Sub
...
私人Sub SomeVBASub
取消= False
DoStuff
如果取消然后退出Sub
DoAnotherStuff
如果取消然后退出Sub
AndFinallyDothis
End Sub
Say I have a button embedded into my spreadsheet that launches some VBA function.
Private Sub CommandButton1_Click()
SomeVBASub
End Sub
Private Sub SomeVBASub
DoStuff
DoAnotherStuff
AndFinallyDothis
End Sub
I'd like to have an opportunity to have some sort of a "cancel" button that would stop SomeVBASub
execution at an arbitrary moment, and I'm not into involving Ctrl+Break
here, 'cause I'd like to do it silently.
I guess this should be quite common issue, any ideas?
Thanks.
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
这篇关于如何停止VBA代码运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!