从被调子项

时间:2018-01-14 19:42:39

标签: excel vba excel-vba if-statement exit

我有一个" main"这称为sub"准备"以及其他子程序。我在"准备"如果满足条件,则退出子。但是,它只退出该特定子程序,然后继续执行" main"中的所有其他子程序。

    If oAltIDLocationDictionary.Exists(sAltID) Then
        MsgBox "It appears that there are two duplicate ID's in your alternate ID list.  Duplicate ID's cannot be processed, please consolidate the location information into a single ID or remove the duplicate ID from the Alt-ID list."
        Exit Sub
    End If

有没有办法退出" main"来自"准备"将其调用,以便在"准备"中满足条件时sub" main"子停止并且不执行其他代码?

2 个答案:

答案 0 :(得分:1)

您可以将subs转换为函数,如果函数返回某个值,则main sub将退出。

Sub Main()
    Dim bTest As Boolean

    ' blah blah

    bTest = Prepare
    If bTest Then Exit Sub

    ' blah blah

End Sub

Function Prepare() As Boolean

    Prepare = False

    If oAltIDLocationDictionary.Exists(sAltID) Then
        MsgBox "It appears that there are two duplicate ID's in your alternate ID list."
        Prepare = True
        Exit Function
    End If

End Function

答案 1 :(得分:0)

要立即停止执行宏,而不返回调用过程,可以使用End语句。