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

标签 vba

我目前有 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/

相关文章:

excel - 有没有一种方法可以计算字符串中每个单词的字符数,并返回以逗号分隔的值?

VBA 宏 - Range 类的 AdvancedFilter 方法失败

vba - 如何在Excel VBA中为图表选择多个范围?

vba - 在 VBA 中动态定义复选框的事件

optimization - 使用 Solver、EXCEL 时保存目标函数值

vba - 使用 vba 从受密码保护的网站中抓取数据用户定义类型未定义

image - 读取图像的像素颜色

vba - Excel Shapes.Range(Array ("someName")) - 定义的范围是什么?

arrays - 如何在 VBA 中调用带有列表和另一个参数的函数?

excel - 设置首选项方法,在 SeleniumVBA 中给出编译错误为 "Argument not optional"