Thread: Stopping Code
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Edward Ulle Edward Ulle is offline
external usenet poster
 
Posts: 92
Default 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 ***