vba 一行宏按名称停止其他宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16042430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:37:29  来源:igfitidea点击:

One line macro to stop other macro by name

excelvbaexcel-vba

提问by Thommy S

I know..

我知道..

Application.Run ("Realcount2")

will call a macro. Can you end a macro with something like

会调用一个宏。你能用类似的东西结束一个宏吗

Application.Stop("Realcount2")

回答by David Zemens

You might be able to do something like this, but I'm not sure how reliable it will be. It seems to work for me on some simple tests, though.

你也许可以做这样的事情,但我不确定它有多可靠。不过,它似乎在一些简单的测试中对我有用。

Declare a public variable like StopMacro

声明一个公共变量,如 StopMacro

Public StopMacro as Boolean

Assign this macro to your Button/etc, which sets a public variable StopMacro.

将此宏分配给您的 Button/etc,它设置了一个公共变量StopMacro

Sub SetStopMacro()

StopMacro = True

End Sub

Here is an example loop structure. Use DoEventsand within the loop, each iteration, check the value of StopMacroand ExitSubif true.

这是一个示例循环结构。使用DoEvents与内环路,每次迭代,检查的价值StopMacroExitSub是否属实。

Sub Macro1()
Dim r As Long
StopMacro = False

For r = 1 To 100000
    DoEvents
    If StopMacro = True Then Exit Sub
    '''''''''''''''''''''''''''''''''''''
    '                                   '
    ' Your code inside the loop         '
    '                                   '
    '''''''''''''''''''''''''''''''''''''
Next

End Sub