从被调用的子程序中退出 VBA 主子程序

Exit VBA Main Subroutine from Called Subroutine

我有一个 sub "main" 调用 sub "prepare" 以及其他子例程。我在 "prepare" 中有一个 if 语句,如果满足条件则退出 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

有没有办法从正在调用的 "prepare" 子程序中退出 "main",以便在 "prepare" 子程序中满足条件时,"main" 子程序停止没有进一步的代码被执行?

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

可以把sub转成函数,如果functionreturn某个值,那么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