Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exit Procedure?

Status
Not open for further replies.

robdunfey

Technical User
Apr 26, 2002
110
GB
Hi,

I am running code and want call a 'exit' sub when I need to exit the currentely executing code. I pass a message to this 'exit' sub that is displayed before quiting the application. The problem is i dont know how to quit the app, End Sub just ends the 'exit sub and returns me to the main code from which it was called, how do I get it to quit the application?

Eg.

Private Sub Main()

Do some stuff

If something Not Type Of something Then Exit_Sub("A message")

Do some more stuff

If something1 Not Type Of something1 Then Exit_Sub("Another type of message")

And do more stuff

End Sub

Private Sub Exit_Sub(message as String)
Set some variables to Nothing
MsgBox message
Exit Sub 'This just returns me to the main sub, I need to kill the app at this point?
End Sub


Any help appreciated,

Rob
 
well - the syntax is
Application.quit
if you want to quit the app or
Workbooks("Workbook_Name").close
if you want to close the workbook

However, there are various issues to deal with if you are doing either of these from code IN the workbook you are going to close

Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
Hi Rob
Note what xlbo has stated.

However in order to achieve what I think you are asking you will need to restructure your code to something like this

Private Sub Main()

Do some stuff

If something Not Type Of something Then
Exit_Sub("A message")
Exit sub 'or quit app or whatever
end if

Do some more stuff

If something1 Not Type Of something1 Then
Exit_Sub("Another type of message")
Exit sub 'or quit app or whatever
end if
And do more stuff

End Sub

Private Sub Exit_Sub(message as String)
Set some variables to Nothing
MsgBox message
'Don't need exit sub here. Let this code return to Main() and deal with your exiting there
End Sub

Exit Sub is basically used to prevent the code that follows it from running. More often used (I think) just before error handling to prevent the handling code from running when there have been no errors

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Rob, in this situation, you can just exit sub in the main code, but here's another idea that perhaps can extend this "problem" to greater application:


Private Function Check_Type(TestVar as Variant, Check as Boolean)

Dim <variables>

If TestVar Not <something> Then
Check = True
End If

End Function



back in main . . .


Private Sub Main()

Dim Check as Boolean
Dim <variables>

{Do some stuff}

Check = False
Call Check_Type(Something1, Check)
If Check = True Then
MsgBox(Message)
Exit Sub
End If


{Do some more stuff}


Check = False
Call Check_Type(Something2, Check)
If Check = True Then
MsgBox(Message)
Exit Sub
End If


{And do more stuff}


End Sub





By keeping a boolean in there, one can reference all sorts of checking functions/subs, and if they come back with the check=true then you can quit the main sub.

Not sure if this is what you wanted, but it's a new direction perhaps for this genre of problem.

Cheers, Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top