it-source

유효성 검사 시 추가 실행을 중지하는 매크로

criticalcode 2023. 6. 15. 21:54
반응형

유효성 검사 시 추가 실행을 중지하는 매크로

나는 있습니다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

댓글 후 편집 : 그냥 사용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

원본 답변:다음 코드에 따라 메인 메서드를 종료하려면 메서드 A를 함수로 만들고 이 함수를 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

언급URL : https://stackoverflow.com/questions/14827273/terminating-macro-from-executing-further-on-validation

반응형