Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 141
Default 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   Report Post  
Posted to microsoft.public.excel.programming
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 ***
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
stopping code Jase Excel Discussion (Misc queries) 1 October 6th 08 05:42 PM
VBA code stopping in odd places JDub Setting up and Configuration of Excel 2 October 10th 06 08:04 AM
Starting/Stopping Worksheet Event Code Paul987 Excel Discussion (Misc queries) 1 March 20th 06 04:43 PM
stopping code from looping tjb Excel Worksheet Functions 3 December 7th 05 02:02 AM
Stopping Code from Running whilst in Loop... Chris Gorham Excel Programming 2 November 21st 05 07:27 AM


All times are GMT +1. The time now is 01:14 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"