+ Reply to Thread
Results 1 to 9 of 9

Code to close active workbook without saving

  1. #1
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Code to close active workbook without saving

    I am trying to create a macro on a userform which displays a message box, which closes the active workbook without saving when yes is clicked.
    The below code is giving me run time error 1004 Method 'Close' of object 'Workbook' failed:

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    04-03-2012
    Location
    Washington State
    MS-Off Ver
    Excel 365
    Posts
    340

    Re: Code to close active workbook without saving

    Hi, how about just ....

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code to close active workbook without saving

    At first it didn't do anything. I then added Unload Me before Application.Quit which then opened the default Do you want to save changes prompt. Iam trying close the workbook without that prompt displaying so the user cannot save the changes.

  4. #4
    Registered User
    Join Date
    02-23-2014
    Location
    Michigan, US
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Code to close active workbook without saving

    http://www.excel-vba.com/vba-code-2-4-workbooks.htm
    If you want to close the workbook within which your VBA procedure (macro) runs without saving it you will write these two lines of code:
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code to close active workbook without saving

    That doesn't seem to work either. I have attached my workbook. The Userform that automatically opens is called Users. If the user doesn't login and clicks the EXIT button I would like the workbook to close down. I can't get the default save changes prompt to stop displaying before closing.
    Attached Files Attached Files

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Code to close active workbook without saving

    The original code you posted worked for me just fine if I put it after Unload Me in the click event of the Exit button.
    Last edited by Norie; 02-23-2014 at 03:12 PM.
    If posting code please use code tags, see here.

  7. #7
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Code to close active workbook without saving

    hi Nitefox
    for "exit" button you can try this
    Please Login or Register  to view this content.
    not sure but this one may be
    Please Login or Register  to view this content.
    Regards, John55
    If you have issues with Code I've provided, I appreciate your feedback.
    In the event Code provided resolves your issue, please mark your Thread as SOLVED.
    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

    ...enjoy -funny parrots-

  8. #8
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code to close active workbook without saving

    Thank you John. I initially didn't want to give the user the option to save (since its not needed as the workbook just opened), and if they hit cancel I was planning on loading the Users userform again. However that at least stops the user from being presented with two message boxes.

  9. #9
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Code to close active workbook without saving

    and just for yr info see this thread, it's an option, http://www.excelforum.com/excel-gene...he-sheets.html

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Code to detect previous active workbook instead of current active workbook
    By kosherboy in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 02-25-2014, 01:58 AM
  2. Close workbook without saving after msg box
    By RebeccaEllis in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2012, 08:41 AM
  3. [SOLVED] Edit code to close all workbooks except active workbook and other specific workbook
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2012, 09:29 PM
  4. Renaming Active.Workbook and saving it without saving Macro
    By Djwill in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-04-2012, 03:09 AM
  5. code:Close without saving
    By LoveCandle in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-26-2006, 04:59 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1