在被调用的子例程中结束宏

Ric*_*man 5 excel vba nested excel-vba routines

我有一个宏(CMOV)调用另一个子程序(CMOV2),它检查一个条件,如果满足,它显示一个vbokaycancel消息框,我设置等于一个名为irep的变量,

我想要它,如果有人点击取消(irep = 2)它取消整个宏.这不仅是退出CMOV2而且退出CMOV.

目前,我有

If jackal(1) <> vbNullString Then
    irep = MsgBox("Warning: You have a selection with two swingarms" _
          & " that are on the same radius and cannot swing past one another " _
          & Chr$(13) & " Choose Okay if you still wish to proceed otherwise " _
          & " choose Cancel to revise your order" & Chr$(13) & "         " _
          & jackal(1) & Chr$(13) & "      " & jackal(2), vbOKCancel)
    **If irep = 2 Then
    Exit Sub**
    Else: End If
    Else: End If
End Sub
Run Code Online (Sandbox Code Playgroud)

在子程序结束时.问题是即使这退出了CMOV2,CMOV也会继续运行.有没有办法结束这个sub,还有一个调用它的方法?

Cor*_*mey 8

End
Run Code Online (Sandbox Code Playgroud)

请注意,End完全停止代码执行(在当前调用堆栈中,因此它不会影响其他项目,如Addins等(一件好事))但它将关闭所有打开的文件句柄(一件好事).另一方面,静态和模块级变量(如果使用它们)将丢失它们的值,并且不会运行类终止方法,因此如果你有更多的'app'而不是宏,这可能是不可取的.

这听起来像你的目的这是好的,可能是最简单的方法.

一个愚蠢的例子:

Sub foo()
    Dim i As Long
    For i = 0 To 10
        If i = 2 Then
            Debug.Print "hooray"
            End
        Else
            Debug.Print "hip"
        End If
    Next i
End Sub
Run Code Online (Sandbox Code Playgroud)


Sid*_*out 5

True如果用户按下取消,则在顶部声明一个布尔变量并将其设置为.这是一个例子.

Dim ExitAll As Boolean

Sub CMOV()
    '
    '~~> Rest of the code
    '

    ExitAll = False

    CMOV2

    If ExitAll = True Then Exit Sub

    MsgBox "Hello World"

    '
    '~~> Rest of the code
    '
End Sub

Sub CMOV2()
    '
    '~~> Rest of the code
    '
    If jackal(1) <> vbNullString Then
        irep = MsgBox("Some Message", vbOKCancel)
        If irep = 2 Then
            ExitAll = True
            Exit Sub
        End If
    End If
    '
    '~~> Rest of the code
    '
End Sub
Run Code Online (Sandbox Code Playgroud)