2013-02-12 25 views
10

進一步執行我有一個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 

回答

14

編輯: 只需使用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的功能,如果你想退出的主要方法按下面的代碼返回此功能爲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 
+0

這個問題方法是我有多個函數調用,而不只是1層函數調用。所以這種可變的方法是不實際的 – 2013-02-12 08:10:41

+0

更新後的答案是什麼? – CuberChase 2013-02-12 08:24:04

+0

ohhh可能是...讓我檢查 – 2013-02-12 09:05:17