2013-02-12 7 views
10

にさらに実行からマクロを終了、私はマクロを終了しなければなりません。私は方法-A状態で複数のメソッドから呼び出される<code>method-A()</code>、</p> <p>を有する検証

私は1つのオプションを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機能作り、次のコードに従って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 
+0

問題アプローチは、私は複数の関数呼び出しを持っているだけでなく、関数呼び出しの1つの層です。この可変的なアプローチは実用的ではありません –

+0

更新された答えはあなたの後にありますか? – CuberChase

+0

オハイオ州のことができます...私は –

関連する問題

 関連する問題