Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ending a macro within a called subroutine

I have a macro (CMOV) which calls another subroutine (CMOV2) that checks for a condition which, if met, displays a vbokaycancel message box which i set equal to a variable called irep,

I want it if someone hits cancel (irep=2) for it to call off the whole macro. That is not only exit CMOV2 but also exit CMOV.

Currently, I have

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

at the end of the subroutine. The issue is that even though this exits the CMOV2, CMOV continues to run. Is there a way to end this sub, and the one which called it?

like image 237
PocketLoan Avatar asked Sep 13 '13 18:09

PocketLoan


People also ask

How do you end a macro?

If the Macro is simply in a continuous loop or is running for too long you can use one of these keyboard shortcuts to kill it: Esc hit the Escape key. Ctrl + Break hit Ctrl key and then the break key, which is also the pause key.

How do you use an Exit Sub?

Using Exit Sub Statement in VBAFirst, decide on which line you want to add the “Exit Sub”. After that, check the structure of the code that will get executed when you run the code. Next, enter the “Exit Sub”. In the end, it's better to have comment that describes why you are using the “Exit Sub” statement.

How do you jump to the end of a sub in VBA?

VBA GoTo End You can also exit a sub using the goto statement. In the above code, we have used the “last” tag just before the end statement and when VBA reaches the GoTo statement, it makes VBA jump to the “Last” at the end of the procedure.

How do you stop a macro in VBA?

Stopping a Procedure To break the running VBA program, do one of the following: On the Run menu, click Break. On the toolbar, click Break Macro icon. Press Ctrl + Break keys on the keyboard.


1 Answers

End

Note that End completely stops code execution (within the current call stack so it doesn't effect other projects like Addins etc (a good thing)) but it will close any open file handles (a good thing). On the other hand, , static and module level variables (if you use them) will lose their values and Class terminate methods won't be run so if you have more of an 'app' than a macro this may not be desired.

It sounds like for your purposes this is ok and is probably the simplest way to go about it.

A silly example:

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
like image 166
Cor_Blimey Avatar answered Sep 28 '22 16:09

Cor_Blimey