Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Len Len is offline
external usenet poster
 
Posts: 162
Default Exit Sub from Main Sub

Hi,

I try to find ways on how to set vba code that when anyone or more
than one of subroutines has prompted "Exit Sub" will exit the main sub
program

E.g

Sub main()
.....
.....
.....
Call abc
End Sub

Sub abc()
.....
.....
Exit Sub
.......
End Sub

Sub def()
.....
.....
Exit Sub
.......
End Sub

Any helps will be appreciated and thanks in advance

Regards
Len
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Exit Sub from Main Sub


You need to return from the lower level sub's an error parameter. Yo
ucan either turn these subs into function or return and error parameter
as a byref variable in the parameter list



sub Main()

Error = Funct1()
if Error then exit sub

end if

end sub

function Funct1()

Funct1 = True

end function


or
------------------------------------------

sub Main()

Call Funct1(MyError)
if MyError then exit sub

end if
end sub

sub Funct1(MyError)

MyError = True

end function


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=190457

http://www.thecodecage.com/forumz

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 834
Default Exit Sub from Main Sub

Here is one way raising an error

Sub main()

On Error GoTo Exit_Main

Call abc

Exit_Main:
'add any tear-down code
Exit Sub

Exit_Error:
If Err.Number = 99999 Then Resume Exit_Main
MsgBox Err.Description
Resume Exit_Main
End Sub

Sub abc()

'do more stuff
If Time = TimeValue("12:00:00") Then Err.Raise 9999

MsgBox "Earlier than 12"

Call def

End Sub

Sub def()

'do stuff

If Time = TimeValue("11:00:00") Then Err.Raise 9999

MsgBox "Earlier than 11"

End Sub



--

HTH

Bob

"Len" wrote in message
...
Hi,

I try to find ways on how to set vba code that when anyone or more
than one of subroutines has prompted "Exit Sub" will exit the main sub
program

E.g

Sub main()
....
....
....
Call abc
End Sub

Sub abc()
....
....
Exit Sub
......
End Sub

Sub def()
....
....
Exit Sub
......
End Sub

Any helps will be appreciated and thanks in advance

Regards
Len



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Exit Sub from Main Sub

I like to use a variable named StopCode. Before your main sub starts, by
default it is False, but set it to True just before you exit the sub or
whenever you like. You can declare the variable at the top of the module if
all of these subs are in one module. If they are in multiple modules you
will have to declare StopCode as a Public variable. Hope this helps! If so,
click "YES" below.

For example,

Dim StopCode As Boolean

Sub main()
......
......
......
Call abc
If StopCode = True Then Exit Sub
......
......
Call def
If StopCode = True Then Exit Sub
......
......
End Sub

Sub abc()
......
......
StopCode = True
Exit Sub
........
End Sub

Sub def()
......
......
StopCode = True
Exit Sub
........
End Sub

--
Cheers,
Ryan


"Len" wrote:

Hi,

I try to find ways on how to set vba code that when anyone or more
than one of subroutines has prompted "Exit Sub" will exit the main sub
program

E.g

Sub main()
.....
.....
.....
Call abc
End Sub

Sub abc()
.....
.....
Exit Sub
.......
End Sub

Sub def()
.....
.....
Exit Sub
.......
End Sub

Any helps will be appreciated and thanks in advance

Regards
Len
.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default Exit Sub from Main Sub

Just put 'End' before 'Exit Sub'.

If all you want is to stop the main procedure from running when a
specific condition is met and the 'Exit Sub' happens, then you can
just put 'End' before every 'Exit Sub' and it will terminate all
running subs.
e.g.:

Sub Main()
Call abc
Call def
End Sub

Sub abc()
...
End ' <- this 'End' stops every currently running sub (including
Main)
Exit sub
End sub

Sub def()
...
End ' <- this 'End' stops every currently running sub (including
Main)
Exit sub
End sub


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Exit Sub from Main Sub

I don't know if I would recommend using End. Because using End will clear
any Public variables values, or close any userforms that he may want to
remain open. I would recommend using End if you want "absolutely everything"
to stop.
--
Cheers,
Ryan


"AB" wrote:

Just put 'End' before 'Exit Sub'.

If all you want is to stop the main procedure from running when a
specific condition is met and the 'Exit Sub' happens, then you can
just put 'End' before every 'Exit Sub' and it will terminate all
running subs.
e.g.:

Sub Main()
Call abc
Call def
End Sub

Sub abc()
...
End ' <- this 'End' stops every currently running sub (including
Main)
Exit sub
End sub

Sub def()
...
End ' <- this 'End' stops every currently running sub (including
Main)
Exit sub
End sub
.

  #7   Report Post  
Posted to microsoft.public.excel.programming
Len Len is offline
external usenet poster
 
Posts: 162
Default Exit Sub from Main Sub

Hi All,

Sorry for delay in replying my post as I was away in the weekend
Thanks for your great helps and your suggestions

I will try it out

Regards
Len



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
Main Sheet Ranjith Kurian[_2_] Excel Programming 6 May 30th 09 02:01 PM
Exit Field vs Exit Button...... roy_ware Excel Programming 2 October 10th 07 04:05 PM
Main first Jenni_Sweden Excel Programming 5 June 15th 06 11:12 AM
Main menu is missing Deb G Excel Discussion (Misc queries) 3 May 10th 05 07:46 PM
If a called sub exit, how to the caller exit right away? luvgreen[_4_] Excel Programming 4 February 24th 04 05:06 PM


All times are GMT +1. The time now is 06:41 PM.

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

About Us

"It's about Microsoft Excel"