Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Stopping Code
I have a 4 functions that work together
Function 1 will prompt the user for a yes\no answer and if yes will run the function and then move to function2 and if no will move straight to function 2 and so on, the problem is function 4, if I answer no it returns to function 3 and runs from where it was called from. Is there anyway I can have the code just stop if the user says no in function 4 thanks |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Stopping Code
Nigel,
There are several options available. 1) Set a global variable in your project for example. Dim CONTINUE_FUNCTIONS As Boolean Depending on the user selection set the variable to true or false and test it upon returning from each level of functions. Function1 ' Call the first level If Not CONTINUE_FUNCTION Then Exit Function 2) Set the return type of the function to Boolean and set it to true or false testing it upon return from the function. This is my prefered method. Function Function1(...) As Boolean Then in the calling procedure. If Not Function1(...) Then Exit Function 3) Or you could use error handling as shown in the following example. This assumes you have no other error handling in your functions. Notice the error handling is actually set at the highest level of your functions. It remains in scope throughout the calling of the sub functions, and it is envoked by the Err.Raise in Function4. Caution, some error numbers are reserved by VB but 1001 to 30000 are open. Option Explicit Sub Test() Function1 MsgBox "Returned from Function1" End Sub Function Function1() On Error GoTo Return_To_Main If MsgBox("Continue", vbYesNo) = vbYes Then MsgBox "Doing something in Function1" End If Function2 MsgBox "Returned from Function2" Return_To_Main: End Function Function Function2() If MsgBox("Continue", vbYesNo) = vbYes Then MsgBox "Doing something in Function2" End If Function3 MsgBox "Returned from Function3" End Function Function Function3() If MsgBox("Continue", vbYesNo) = vbYes Then MsgBox "Doing something in Function3" End If Function4 MsgBox "Returned from Function4" End Function Function Function4() If MsgBox("Continue", vbYesNo) = vbYes Then MsgBox "Doing something in Function4" Else Err.Raise 1001 End If End Function *** Sent via Developersdex http://www.developersdex.com *** |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Stopping Code
you can just put an "end " in if a=1 then end else dosomething end if but such poorly structured code would, in my eyes, earn you your cowboy boots, hat and spurs. -- tony h ------------------------------------------------------------------------ tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074 View this thread: http://www.excelforum.com/showthread...hreadid=503340 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
stopping code | Excel Discussion (Misc queries) | |||
VBA code stopping in odd places | Setting up and Configuration of Excel | |||
Starting/Stopping Worksheet Event Code | Excel Discussion (Misc queries) | |||
stopping code from looping | Excel Worksheet Functions | |||
Stopping Code from Running whilst in Loop... | Excel Programming |