Consulting

Results 1 to 11 of 11

Thread: Close ss with confirmation

  1. #1

    Close ss with confirmation

    Is it possible to close a ss without it asking if you want to save changes? I tried doing a macro but closing the ss made the recorder go away.

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This will close, without asking, without saving any changes
    [VBA]With ThisWorkbook
    .Saved = True
    .Close
    End With[/VBA]

    This will close, saving any changes, without asking
    [VBA]ThisWorkbook.Close SaveChanges:=True[/VBA]

  3. #3
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    Codeblue,
    The big question is do you want to save the workbook changes or not...

    If you want to save the workbook changes then:
    [VBA]Sub test()
    ThisWorkbook.Saved = True
    ThisWorkbook.Save
    ThisWorkbook.Close

    End Sub[/VBA]

    If you do NOT want to save the changes then

    [VBA]Sub test2()
    ThisWorkbook.Saved = True
    ThisWorkbook.Close

    End Sub[/VBA]

    Hope one of these answers your question.

  4. #4
    Yes, this is great stuff. I'm not using a button to close the ss just the X in the upper right corner so I'm not sure where to place the code.

    If I put this in my routine that opens my ss my userform goes away, which I don't want, then I close my ss it still asks if I want to save.

    I should mention I am using a userform

  5. #5
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    You could always put it in worksheet_change()

  6. #6
    I tried this but it doesn't work
    Public Sub worksheet_change(ByVal Target As Range)
            ThisWorkbook.Saved = True
            ThisWorkbook.Close
    End Sub
    Last edited by Codeblue; 06-26-2012 at 10:09 AM.

  7. #7
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    Better yet... lol... do it before workbook close...

    Go into vbe, double click on the workbook excel object enter this...

    Just know that this does NOT save changes to the workbook... so you will loose your changes if you close the workbook.

    [VBA]Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ThisWorkbook.Saved = True
    End Sub[/VBA]


    Try it, see if it works for you.

  8. #8
    You may need to talk to me like a 5 yr old cause I don't got it.
    By workbook excel object you mean in my project explorer the one that has a default name of Thisworkbook? I put the code in there but it still asks about saving it or not. It may be confused since I have a feature of opening a ss with data that opens on top of my ss with my form on it. It does work when closing that ss.

    Perhaps I need to make the opened ss with data the active ss then when closing have the active one execute your code. Or am I all wet here.

  9. #9
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    Why don't you attach your workbook so I can take a look at it... maybe something is affecting it... If not, I can adjust and send so you can see where it goes..

    You could attach it without any data...

  10. #10
    Here is my project I had to delete some company sensitive info so it is a bit of a mess. Thanks for taking the time to look at this.
    Attached Files Attached Files

  11. #11
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    Codeblue,

    1- I removed ThisWorkbook.ActiveSheet from the workbook_beforeclose
    2- in your user form frmmain, there is a close app... if you want this to close the entire excel program from there, you will need to close the workbook there...
    a- I have added a commented out thisworkbook.close which you can remove the ' and then this will close the workbook under that condition.
    The way this is set now, if you close the workbook, it will not ask for a save, and it will not save the workbook automatically. This can result in lost data.

    Hope this helps.

    multi demo no code-1.xlsm

Posting Permissions

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