Results 1 to 7 of 7

exiting a sub within a sub

This is a discussion on exiting a sub within a sub within the Excel Questions forums, part of the Question Forums category; hey i have a module which calls another one. let's say Sub A() call B end sub sub B() exit ...

  1. #1
    Board Regular
    Join Date
    May 2002
    Location
    mtl, canada
    Posts
    160

    Default

    hey
    i have a module which calls another one. let's say
    Sub A()
    call B
    end sub

    sub B()
    exit sub
    end sub

    i want to be able to exit both B and A sub. is that possible?

    thanks in advance


  2. #2
    MrExcel MVP phantom1975's Avatar
    Join Date
    Jun 2002
    Location
    Omaha, Nebraska
    Posts
    3,940

    Default

    With the subs you have listed, sub A will run and close and so will sub B. If you have a UserForm that is called up within sub A, I believe it will still be showing until sub B is done. If your specific issue is having the UserForm disappear, you may want to use an Unload UserForm command just before the Call statement.

    Did this help?
    Silly Billy was here....

    ***************** EXCEL/VB NEWBIES ARE MY FAVORITE! *****************

  3. #3
    Board Regular
    Join Date
    May 2002
    Location
    mtl, canada
    Posts
    160

    Default

    hi thanks!
    in fact there is code in sub A after the call of sub B so it's more like:
    sub A()
    call B
    ....(code)....
    end sub
    sub B
    ...(code)...
    exit sub
    end sub

    i don't want the code after the call B to be executed.
    is that possible?


  4. #4
    MrExcel MVP phantom1975's Avatar
    Join Date
    Jun 2002
    Location
    Omaha, Nebraska
    Posts
    3,940

    Default

    The mystery is why you would even write code after the Call B in Sub A. You will probably need to qualify your code within Sub A so that the code knows whether to Call Sub B or to continue with Sub A. You will probably want to do something like this:

    If (WHATEVER YOUR CONDITION IS) Then
    Call Sub B
    Exit Sub
    End If

    Does that help?
    Silly Billy was here....

    ***************** EXCEL/VB NEWBIES ARE MY FAVORITE! *****************

  5. #5
    MrExcel MVP Richie(UK)'s Avatar
    Join Date
    May 2002
    Location
    UK
    Posts
    3,330

    Default

    Hi Tealeaf,

    How about passing a variable from Sub B to Sub A (obviously placed before the Exit Sub line!) and then using an If..Then test in Sub A such that if the variable is True then Exit Sub?

    Eg Dim fCloseTest as Boolean (Module level variable)

    Then in Sub B use fCloseTest=True and,
    in Sub A If fCloseTest=True Then Exit Sub
    (set the variable to False at the start of Sub A).

    Would this work for you?
    Richie

  6. #6
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,945

    Default

    The drastic solution would be to use End instead of Exit Sub
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  7. #7
    Board Regular
    Join Date
    May 2002
    Location
    mtl, canada
    Posts
    160

    Default

    On 2002-08-29 08:35, Juan Pablo G. wrote:
    The drastic solution would be to use End instead of Exit Sub
    in fact this is the solution to my problem
    i just want to thank everyone who helped. i chose the less elegant solution i think...but it works and it's fast. so i'm glad.
    thanks again

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com