Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default VBA Runs After Application.Quit

Can someone please explain why Application.Quit is ignored
here and VBA goes on working? This is on Excel 97. My
code is similar to this:

Sub My_subrtn()

Application.DisplayAlerts = False

Dim x As Integer
x = 3

If x 2 Then

Application.Quit

End If

MsgBox "VBA is still running"

MsgBox "VBA is still running"

MsgBox "VBA is still running"

End Sub


Here, Application.Quit is ignored(?) and all the MsgBoxes are
executed.

I googled this and found that a DoEvents after the
Application.Quit statement solves the problem. Does anyone
know why?

I ask because I always thought that each statement in the
program must be executed before the next statement enters
the program. The above behavior throws that out the window.
In this case for example, do I need a DoEvents statement
after x = 3 in order to make sure the If statement doesn't
execute first? In other words what is it that makes
Application.Quit different?

Thank you for your time.


Andy
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA Runs After Application.Quit

Op 16-4-2011 19:41, Andy schreef:
Can someone please explain why Application.Quit is ignored
here and VBA goes on working? This is on Excel 97. My
code is similar to this:

Sub My_subrtn()

Application.DisplayAlerts = False

Dim x As Integer
x = 3

If x 2 Then

Application.Quit

End If

MsgBox "VBA is still running"

MsgBox "VBA is still running"

MsgBox "VBA is still running"

End Sub


Here, Application.Quit is ignored(?) and all the MsgBoxes are
executed.

I googled this and found that a DoEvents after the
Application.Quit statement solves the problem. Does anyone
know why?

I ask because I always thought that each statement in the
program must be executed before the next statement enters
the program. The above behavior throws that out the window.
In this case for example, do I need a DoEvents statement
after x = 3 in order to make sure the If statement doesn't
execute first? In other words what is it that makes
Application.Quit different?

Thank you for your time.


Andy


Hi Andy,

Not realy shore why the subroutine is continued, but i think it's
because excel wait for the subroutine to be finished before it closes.

maybe you shoot ad Exit Sub after the Application.Quit ....

Sub My_subrtn()
Application.DisplayAlerts = False
Dim x As Integer
x = 3
If x 2 Then
Application.Quit
Exit Sub
End If
MsgBox "VBA is still running"
MsgBox "VBA is still running"
MsgBox "VBA is still running"
End Sub


Gerard
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default VBA Runs After Application.Quit

On Mon, 18 Apr 2011 11:31:58 +0200, GerardV
wrote:

Hi Andy,

Not realy shore why the subroutine is continued, but i think it's
because excel wait for the subroutine to be finished before it closes.

maybe you shoot ad Exit Sub after the Application.Quit ....

Sub My_subrtn()
Application.DisplayAlerts = False
Dim x As Integer
x = 3
If x 2 Then
Application.Quit
Exit Sub
End If
MsgBox "VBA is still running"
MsgBox "VBA is still running"
MsgBox "VBA is still running"
End Sub


Gerard


Thanks for the reply Gerard. Even if you Exit Sub after the
Application.Quit, VBA is still running. Here is your subroutine with
a minor modification - This is on Excel 2000:

Sub My_subrtn()
Application.DisplayAlerts = False
Dim x As Integer
x = 3
If x 2 Then
Call JustQuit
End If
MsgBox "VBA is still running"
MsgBox "VBA is still running"
MsgBox "VBA is still running"
End Sub

Sub JustQuit()
Application.Quit
End Sub

Again, thanks for the reply.

Andy
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default VBA Runs After Application.Quit

Try closing all workbooks before calling quit.
Something as simple as...
Workbooks.Close
Application.Quit

If you are automating Excel, then you have to call quit on the object reference not "Application".
--
Jim Cone
Portland, Oregon USA .
http://www.mediafire.com/PrimitiveSoftware .
(Bingo Card Variety, Excel workbook - in the free folder)


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default VBA Runs After Application.Quit

On Tue, 19 Apr 2011 21:16:31 -0700, "Jim Cone"
wrote:

Try closing all workbooks before calling quit.
Something as simple as...
Workbooks.Close
Application.Quit

If you are automating Excel, then you have to call quit on the object reference not "Application".


Good tip - thank you. To make it work I ended up closing the
workbooks one by one - something like:

ActiveWorkbook.Close Savechanges:=False
Workbooks("Name_01.xls").Close Savechanges:=False

(Still working on this - the last workbook for some reason is not
closing)

As an aside, I always thought that "Application.Quit" will close all
workbooks no questions asked.

Again, Thank you.

Andy


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default VBA Runs After Application.Quit

"Still working on this - the last workbook for some reason is not closing"
Corrupted workbooks are a problem and what they will do to workbook code is unpredictable.

I believe having multiple versions of Excel on your machine can help identify problems.
Sometimes that smoothly running piece of code has difficulties in another version.
(even if the "other" version will not be used to run the finished program, it pays to check)
--
Jim Cone
Portland, Oregon USA .
http://www.mediafire.com/PrimitiveSoftware .
(Extras for Excel add-in: convenience built-in)





"Andy"
wrote in message
...
On Tue, 19 Apr 2011 21:16:31 -0700, "Jim Cone"
wrote:

Try closing all workbooks before calling quit.
Something as simple as...
Workbooks.Close
Application.Quit

If you are automating Excel, then you have to call quit on the object reference not "Application".


Good tip - thank you. To make it work I ended up closing the
workbooks one by one - something like:

ActiveWorkbook.Close Savechanges:=False
Workbooks("Name_01.xls").Close Savechanges:=False

(Still working on this - the last workbook for some reason is not
closing)

As an aside, I always thought that "Application.Quit" will close all
workbooks no questions asked.

Again, Thank you.

Andy



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default VBA Runs After Application.Quit

The behavior of the Application.Quit statement is what it is, and so we
need to work with that! In all probability, Excel has a 'Terminate'
event that runs before shutdown occurs.

I'm not sure why you have executable code after the Application.Quit
statement but that's rather unusual. This is a normal statement for a
shutdown routine that also handles menus, toolbar, and UI settings
cleanup as well as saving of any open workbooks, BUT this is always the
last executable line in the shutdown routine.

Also, the shutdown routine might begin with 'On Error Resume Next'
since at this point it's too late to mitigate any errors that might
occur.

Are you actually using this statement in a project OR just messing
around with it for curiosity?

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default VBA Runs After Application.Quit

On Wed, 20 Apr 2011 01:19:34 -0400, GS wrote:

The behavior of the Application.Quit statement is what it is, and so we
need to work with that! In all probability, Excel has a 'Terminate'
event that runs before shutdown occurs.

I'm not sure why you have executable code after the Application.Quit
statement but that's rather unusual. This is a normal statement for a
shutdown routine that also handles menus, toolbar, and UI settings
cleanup as well as saving of any open workbooks, BUT this is always the
last executable line in the shutdown routine.


In this case the need to shut down is the result of an error in the
spreadsheet. If it does not shutdown then it mails itself to a
number of users who are now receiving the wrong information.


Also, the shutdown routine might begin with 'On Error Resume Next'
since at this point it's too late to mitigate any errors that might
occur.

Are you actually using this statement in a project OR just messing
around with it for curiosity?


I did not think about 'On Error Resume Next' - I will give it a shot.

And no, this is not a curiosity item - I was very surprised to
discover that 'Application.Quit' is to some extend ignored by VBA
and Excel. I understand that it depends on the Operating System and I
am not clear yet on how to deal with this.

Thank you for the comments.

Andy
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default VBA Runs After Application.Quit

After serious thinking Andy wrote :
On Wed, 20 Apr 2011 01:19:34 -0400, GS wrote:

The behavior of the Application.Quit statement is what it is, and so we
need to work with that! In all probability, Excel has a 'Terminate'
event that runs before shutdown occurs.

I'm not sure why you have executable code after the Application.Quit
statement but that's rather unusual. This is a normal statement for a
shutdown routine that also handles menus, toolbar, and UI settings
cleanup as well as saving of any open workbooks, BUT this is always the
last executable line in the shutdown routine.


In this case the need to shut down is the result of an error in the
spreadsheet. If it does not shutdown then it mails itself to a
number of users who are now receiving the wrong information.


Could you not recode the procedure that does this to abort if the error
exists, giving the user an instruction as to how to correct the error
maybe. I'm thinking it might be easier to exit a sub/function than code
for all the nuances surrounding a shutdown.<g



Also, the shutdown routine might begin with 'On Error Resume Next'
since at this point it's too late to mitigate any errors that might
occur.

Are you actually using this statement in a project OR just messing
around with it for curiosity?


I did not think about 'On Error Resume Next' - I will give it a shot.

And no, this is not a curiosity item - I was very surprised to
discover that 'Application.Quit' is to some extend ignored by VBA
and Excel. I understand that it depends on the Operating System and I
am not clear yet on how to deal with this.

Thank you for the comments.

Andy


--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


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
Application.Quit Does not Close the Application [email protected] Excel Programming 6 October 8th 09 10:32 PM
difference application.quit & application.close Pierre via OfficeKB.com[_2_] Excel Programming 4 November 8th 05 07:55 PM
macro to close excel application other than application.quit mary Excel Programming 1 September 14th 04 03:43 PM
application.quit will not shut off application john Excel Programming 0 January 9th 04 11:29 PM
Quit Application Robert Black Excel Programming 1 July 31st 03 04:15 PM


All times are GMT +1. The time now is 10:26 AM.

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"