PDA

View Full Version : Exit from Sub which is called from another Sub



prayag2015
01-19-2016, 04:10 AM
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

GTO
01-19-2016, 04:49 AM
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

SamT
01-19-2016, 04:57 AM
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

GTO
01-19-2016, 05:17 AM
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

SamT
01-19-2016, 05:43 AM
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. :rofl:

GTO
01-19-2016, 06:04 AM
@SamT:

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

prayag2015
01-19-2016, 10:04 AM
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..

SamT
01-19-2016, 01:34 PM
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

prayag2015
01-20-2016, 12:29 AM
Thanks SamT,
this worked well...
:clap2: