终止宏从验证进一步执行 [英] Terminating Macro From executing further on validation

查看:114
本文介绍了终止宏从验证进一步执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a method-A() that is called from multiple methods,

方法A,我必须终止宏。

On a condition in method-A, i have to terminate the macro.

我看到一个选项为退出子,但这只会退出当前 sub ie:method-A(),剩下的程序继续。

i saw one option as Exit sub but this will just exit the current sub ie:method-A() and the remaining program continues.

如何处理这个。

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 您要终止所有代码的位置。

Edit after comment: Just use end where you want to terminate ALL code.

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

:所有你需要做的是make methodA是一个函数,如果你想按照以下代码退出main方法,返回这个函数为FALSE:

Original Answer: All you need to do is make methodA a function and return this function as FALSE if you want to exit the main method as per the following code:

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

这篇关于终止宏从验证进一步执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆