gpt4 book ai didi

vba - 嵌套宏 : Scope: how to exit all?

转载 作者:行者123 更新时间:2023-12-04 11:39:45 25 4
gpt4 key购买 nike

我目前有 2 个宏:

在第一个宏中调用第二个宏来执行任务。但是我在第二个宏中有逻辑说明我的变量 最后一行 < 3 然后退出子。这当然让我们立即回到宏 1。我希望在这里也立即退出宏 1。我尝试这样做的方法是在两个宏中公开 LastRow ..所以当我们退出到宏 1 时,我们有:

sub macro1()
application.run("macro2")
if LastRow < 3 then
exit sub
end sub

其中宏 2()
sub macro1()

Static LastRow As Long

if LastRow < 3 then
exit sub
else do something
end if
end sub

我相信我的问题可能是 静态 没有让宏 1 访问变量 LastRow。

最好的方法是什么?

问候!

最佳答案

您可以使用 End statement通过这种方式:

sub macro2()

Static LastRow As Long

if LastRow < 3 then
'...here is End
End
else
'do something
end if
end sub

然而, End有一些你应该注意的缺点。让我根据 MSDN 引用它们:

Terminates execution immediately. Never required by itself but may be placed anywhere in a procedure to end code execution, close files opened with the Open statement and to clear variables.

When executed, the End statement resets all module-level variables and all static local variables in all modules. To preserve the value of these variables, use the Stop statement instead. You can then resume execution while preserving the value of those variables.

The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.

关于vba - 嵌套宏 : Scope: how to exit all?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16895948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com