Consulting

Results 1 to 9 of 9

Thread: Exit from Sub which is called from another Sub

  1. #1

    Exit from Sub which is called from another Sub

    hi ,
    pl help me wit the code. i want to end the SUB which is called from another Sub.
    here in my example, their is Main Sub. in that Process1() sub us called. i f condition if not matched then i want to exit from Process sub as well as Main Sub both.


    Sub Main()
         Process1
         codes ....
         codes...
    End Sub
    
    Sub Process1()
    If Activecell.value <> 1 Then Exit Sub
    Codes .....
    Codes.....
    End Sub
    Last edited by SamT; 01-19-2016 at 04:51 AM. Reason: Added CODE Tags with # Menu Icon

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    If I am understanding your question/quandary, try making Process1 a Function that returns TRUE or FALSE. Something like:

    Option Explicit
      
    Sub Main()
      If Process1 Then
        MsgBox "More codes ...."
      End If
    End Sub
      
    Function Process1() As Boolean
      
      If ActiveCell.Value <> 1 Then
        Process1 = False '<Just clarity as the default is already False
        Exit Function
      End If
      
      MsgBox "Codes ....."
      
      Process1 = True
      
    End Function
    Hope that helps,

    Mark

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    First I added Code tags, but there were a lot of COLOR and FONT Tags, so I removed them. Then I had to Remove Format from the code and add CODE Tags again.

    Prayag, what program did you copy that code from?


    i f condition if not matched then i want to exit from Process sub as well as Main Sub both.
    Test the condition in Main sub before you call Process Sub.

    If you must test only in Process.
    Sub Main
    If Not Process Then Exit Sub
    '
    '
    '
    End Sub
    
    Function Process() As Boolean
    If Not Condition Then Exit Function
    '
    '
    '
    Process = True
    End Function
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by SamT View Post
    First I added Code tags, but there were a lot of COLOR and FONT Tags, so I removed them. Then I had to Remove Format from the code and add CODE Tags again.
    Hi Sam,

    You mean in your own post or mine?

    Mark

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Mark, In the OP. You posted while I was typing

    But our two posts, blind from each other, proves that your mind is as great as mine.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    @SamT:

    Thanks Sam. I'm not sure how sound the internals to my noggin are most days though...

  7. #7
    hi Sam n GTO,

    thanks for the solution, this also worked for me..
    but instead i used below code to exit from my macro.


    Sub Main()
    dim i: i=0
    ' Ping to a server
    '
    Call Process1 ' to Find some strings....
    if i=1 then Exit Sub
    end if ' this will exit my macro if Login unsuccessful
    ' commands/codes to be send in Telnet session
    End Sub

    Sub Process1()
    ' codes to find a string in the text file...."
    if Found then Exit sub ' this will return back to Main Sub were it was left to continue ahead
    Else
    i=i+1
    End If

    End Sub

    does this will Work..

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Suggest small changes

    Dim B: As Boolean 'Module level Variable
    
    Sub Main() 
    
    ' Ping to a server
    ' 
     Call Process1             ' to Find some strings....
    If B Then Exit Sub 'One line
    
    ' commands/codes to be send in Telnet session
    End Sub 
     
    Sub Process1()
    ' codes to find a string in the text file...." 
    
    If Found Then Exit Sub
    B = True
    End Sub
    The # Button in the Menu insert CODE Tags
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    Thanks SamT,
    this worked well...

Tags for this Thread

Posting Permissions

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