VBA: Exit Without Saving

October 22nd, 2004 | Categories: VBA | Tags:

exitYou can exit or close Excel without asking the user to save by setting display alerts to False and calling Application.Quit. Here’s some simple example code to exit Excel without prompting the user to save:

Sub ExitWithoutPrompt()
     Application.DisplayAlerts = False
End Sub
  1. October 24th, 2004 at 14:47
    Reply | Quote | #1

    but would you want to?????!

  2. Mark
    October 26th, 2004 at 08:38
    Reply | Quote | #2

    Most of the time I post whatever comes to mind or from notes, however sometimes I post relating to people searching this site for topics I haven’t covered, this is the case with this post.

    I can’t think of many practical reasons for doing this however – possibly a calculator template that a user can manipulate but should exit without saving the current state?

  3. December 15th, 2004 at 10:53
    Reply | Quote | #3

    maybe – sounds like trouble to me though! – even if you’ve cerated a new instance of the excel object

  4. Wendell
    November 12th, 2008 at 21:54
    Reply | Quote | #4

    This is just what I was looking for.

  5. Brendan
    December 4th, 2008 at 01:41
    Reply | Quote | #5

    @ross, mark.

    Might not be particularly useful in general Excel use, but has a fantastic use in programming Excel from other applications using COM automation.

    appExcel.DisplayAlerts = False

    Inside the exception procedure if something else gets ballsed up. This prevents having a hundred EXCEL.EXE processes in the background while you’re debugging, because every “New Excel.Application” spawns a new Excel process.

  6. Monit
    May 15th, 2009 at 05:56
    Reply | Quote | #6

    Worked for me… thanks

  7. Keith Bricknell
    June 24th, 2009 at 14:10
    Reply | Quote | #7

    Set up a scheduled task to open excel, query an sql database, create a new (non-query) workbook with the results. Attached the new workbook to an email and sent it.

    works a treat, the only bit I was missing was displayalerts = false!


  8. John D
    August 17th, 2010 at 15:34
    Reply | Quote | #8

    This code really helped me for a small project I’m doing, thanks a bunch!

  9. Scott
    October 19th, 2010 at 16:46
    Reply | Quote | #9

    Another great application for this is for testing string manipulation code, but not preserving the changes.

    Say you want to create some code to remove carriage returns from cells in hundreds of different workbooks, testing the batch code will throw up a “Save changes?” dialog each time you try to close the workbook and access another. By suppressing the warnings (and assuming you are using some sort of internal cataloging method to test that your code is doing it’s job, debug.print being a simple way to do so) you can quickly test the batch-readiness of your code.

    Thanks for the tips!

  10. A Different Scott
    December 6th, 2010 at 16:13

    An application I just had for this:

    I had an Access VBA function open up an instance of excel, make some changes to a file in the background, and bring up a “Save As” dialog. If the user clicks “Cancel” on the dialog, it returns a value of “False”. I tested for this condition so that when the user clicks Cancel and my function eventually tries to call “ExcelAppObj.Quit” it first sets DisplayAlerts = false so that the user doesn’t receive a dialog asking if they’d like to save the changes made to the excel file.

  11. mark
    January 24th, 2011 at 21:58

    i just included this code in an auto open sub to create different views for different business users. (ie it will run different commands if the username is in different lists on a worksheet. (stupidly) i ran the sub with my username on the autoexit list, as part of testing. now whenever i open the file it automatically exits.

    as this is a test file i cannot send it to other users as i am the only name on the list. can anyone think of a workaround for this? i need to be able to get into the macro but without it running

    thanks in advance

  12. Ken
    March 27th, 2011 at 01:19

    You can also use this if you only wish to close the workbook containing the code.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.DisplayAlerts = False
    Workbooks(“receipt tax spreader tool.XLS”).Close
    Application.DisplayAlerts = True
    End Sub

    I used it for a calculator-type file to help me with my finances.

  13. July 15th, 2011 at 09:29

    Perfekt, just what I needed

  14. Nate
    July 15th, 2011 at 18:34

    Is there any way to restart Excel within a VBA macro? I’d like to set up an error handler with a user form that allows the user to reboot Excel or continue.