VBA 中的“结束”和“退出子”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36491908/
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-08 10:20:07  来源:igfitidea点击:

What's the difference between "end" and "exit sub" in VBA?

excelvba

提问by Nicholas

In VBA, sometimes we want to exit the program after some condition is true. But do I use endor exit sub?

在 VBA 中,有时我们想在某些条件为真后退出程序。但是我使用endexit sub吗?

回答by Vegard

This is a bit outside the scope of your question, but to avoid any potential confusion for readers who are new to VBA: Endand End Subare not the same. They don't perform the same task.

这有点超出了您的问题范围,但为了避免对 VBA不熟悉的读者造成任何潜在的混淆:End并且End Sub不一样。它们不执行相同的任务。

Endputs a stop to ALL code execution and you should almost always use Exit Sub(or Exit Function, respectively).

End停止所有代码执行,您几乎应该总是使用Exit Sub(或Exit Function分别)。

End halts ALL exectution. While this sounds tempting to do it also clears all global and static variables. (source)

End 停止所有执行。虽然这听起来很诱人,但它也会清除所有全局和静态变量。(来源

See also the MSDN dox for the End Statement

另请参阅结束声明的 MSDN dox

When executed, the Endstatement resets allmodule-level variables and all static local variables in allmodules. To preserve the value of these variables, use the Stopstatement instead. You can then resume execution while preserving the value of those variables.

Note The Endstatement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events offorms andclass modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

执行时,该End语句会重置所有模块级变量和所有模块中的所有静态局部变量。要保留这些变量的值,请改用该Stop语句。然后,您可以在保留这些变量的值的同时恢复执行。

注意 该End语句会突然停止代码执行,而不调用 Unload、QueryUnload 或 Terminate 事件或任何其他 Visual Basic 代码。放置在窗体和类模块的 Unload、QueryUnload 和 Terminate 事件中的代码不会执行。从类模块创建的对象将被销毁,使用 Open 语句打开的文件将被关闭,程序使用的内存将被释放。其他程序持有的对象引用无效。

Nor is End Suband Exit Subthe same. End Subcan't be called in the same way Exit Subcan be, because the compiler doesn't allow it.

也不是End SubExit Sub相同的。End Sub不能以相同的方式调用Exit Sub,因为编译器不允许。

enter image description here

在此处输入图片说明

This again means you have to Exit Sub, which is a perfectly legal operation:

这再次意味着您必须这样做Exit Sub,这是一个完全合法的操作

Exit Sub
Immediately exits the Sub procedure in which it appears. Execution continues with the statement following the statement that called the Subprocedure. Exit Subcan be used only inside a Subprocedure.

Exit Sub
立即退出出现它的 Sub 过程。继续执行调用Sub过程的语句之后的语句。Exit Sub只能在Sub过程中使用。

Additionally, and once you get the feel for how procedures work, obviously, End Subdoes not clear any global variables. But it does clear local (Dim'd) variables:

此外,一旦您了解了程序的工作方式,显然End Sub不会清除任何全局变量。但它确实清除了本地(Dim'd)变量

End Sub
Terminates the definition of this procedure.

End Sub
终止此过程的定义。