18

我有一个method-A()从多种方法调用的,

在方法 A 中的一个条件下,我必须终止宏。

我看到了一个选项,Exit sub但这只会退出当前sub ie:method-A()程序,其余程序继续。

如何处理。

Sub mainMethod()
    method-A()
end Sub

Sub method-A()
    if (true) Then
         'Terminate the macro. that is exit method-A() and also mainMethod()
end Sub
4

1 回答 1

21

评论后编辑:只需使用end您要终止所有代码的位置。

Sub mainMethod()
    method_A()
end Sub

Sub method-A()
    if (true) Then End
         'Terminate the macro. that is exit method-A() and also mainMethod()
end Sub

原始答案:您需要做的就是使 methodA 成为一个函数,如果您想按照以下代码退出 main 方法,则将此函数返回为 FALSE:

Sub mainMethod()

    'Run the custom function and if it returns false exit the main method
    If Not method_A Then Exit Sub

    'If method_A returns TRUE then the code keeps going
    MsgBox "Method_A was TRUE!"

End Sub

Function method_A() As Boolean
    Dim bSomeBool As Boolean

   'Code does stuff
   bSomeBool = True

   'Check your condition
   If bSomeBool Then
       'Set this function as false and exit
       method_A = False
       Exit Function
   End If

    'If bSomeBool = False then keep going
End Function
于 2013-02-12T07:53:06.717 回答