Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminating Macro From executing further on validation

Tags:

excel

vba

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

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

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

how to handle this.

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
like image 910
Naveen Babu Avatar asked Feb 12 '13 07:02

Naveen Babu


People also ask

How do I stop a macro from execution?

Pressing shift + Escape will be like pressing ctrl + break and thus will stop your macro.

How do I stop a macro execution in Excel VBA?

In VBA, you can stop your macro execution manually with the Esc key or by pressing Ctrl+Break.

Can macro be undone?

It's actually impossible to undo a macro action, which as you can imagine is both inconvenient and potentially dangerous for long term data loss. So when creating and running macros always be aware that the undo action won't be available to you.

How do you exit a loop in a macro?

A Exit For statement is used when we want to exit the For Loop based on certain criteria. When Exit For is executed, the control jumps to the next statement immediately after the For Loop.

How to exit the main method of a macro?

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 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:

How to terminate all code in a macro?

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

Why do some macros have a 2 at the end?

That is why there are also macros with "2" at the end. The ones with no "2" at the end are specific to the first run and the ones with a "2" at the end are specific to the second run. I assume that some additional code will need to be added to the StartTimer macro to achieve what I want to do, but am not 100% sure, thus why I am sharing everything.

How do I report a terminating error in a cmdlet?

When a terminating error occurs, the cmdlet should report the error by calling the System.Management.Automation.Cmdlet.Throwterminatingerror* method. This method allows the cmdlet to send an error record that describes the condition that caused the terminating error. For more information about error records, see Windows PowerShell Error Records.


1 Answers

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

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
like image 176
CuberChase Avatar answered Sep 27 '22 22:09

CuberChase