vba 嵌套宏:范围:如何全部退出?

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

Nested Macros: Scope: how to exit all?

vba

提问by user1717622

I current have 2 Macros:

我目前有 2 个宏:

The second macro is called within the first to perform a task. However I have logic within the second macro that states if my variable LastRow < 3then exit the sub. This of course takes us immediately back into macro 1. What I desire here is to then exit immediately macro 1 as well. The way I have attempted to do this is by making LastRow public within both macros.. so when we exit back into macro 1, we have:

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

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

where macro 2()

其中宏 2()

sub macro1()

    Static LastRow As Long

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

I believe I may the issue may be that Staticis not giving macro 1 access to variable LastRow.

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

whats the best way to proceed?

最好的方法是什么?

Regards!

问候!

回答by Kazimierz Jawor

You could use End statementin this way:

你可以这样使用End statement

sub macro2()

    Static LastRow As Long

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

However, Endhas some disadvantages you should be aware of. Let me cite them base on MSDN:

但是,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.

立即终止执行。本身从不需要,但可以放置在过程中的任何位置以结束代码执行、关闭用 Open 语句打开的文件和清除变量。

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

End 语句提供了一种强制程序停止的方法。对于 Visual Basic 程序的正常终止,您应该卸载所有窗体。只要没有其他程序持有对从您的公共类模块创建的对象的引用并且没有代码执行,您的程序就会关闭。

回答by assylias

You could use a Function instead of a Sub and return a Boolean for example.

例如,您可以使用 Function 而不是 Sub 并返回布尔值。

Function macro2() As Boolean
'returns false if the last row is 2 or less, true otherwise

    LastRow As Long

    if LastRow >= 3 then
        macro2 = True
        'do something
    end if
End Function

Then in your first macro:

然后在你的第一个宏中:

sub macro1()
    if Not macro2 Then Exit Sub
end sub

回答by Dick Kusleika

Declare the variable in the first macro and pass it ByRef to the second macro.

在第一个宏中声明变量并将其 ByRef 传递给第二个宏。

Sub Mac1()

    Dim lLastRow As Long

    Mac2 lLastRow

    If Not IsTooBig(lLastRow) Then
        'do stuff
    End If

End Sub

Sub Mac2(ByRef lLastRow As Long)

    lLastRow = 5

    If IsTooBig(lLastRow) Then
        Exit Sub
    End If

End Sub

Function IsTooBig(ByVal lLastRow As Long) As Boolean

    IsTooBig = lLastRow >= 5

End Function

ByRef means that whatever changes you make to lLastRow in Mac2 will be reflected in lLastRow in Mac1.

ByRef 意味着您在 Mac2 中对 lLastRow 所做的任何更改都将反映在 Mac1 中的 lLastRow 中。