+ Reply to Thread
Results 1 to 2 of 2

Macro to close and save file with message box - error button to be clicked twice

Hybrid View

  1. #1
    Registered User
    Join Date
    02-02-2013
    Location
    NL
    MS-Off Ver
    Excel 2010
    Posts
    63

    Macro to close and save file with message box - error button to be clicked twice

    All, I have a problem. I want to have the user to select whether they want to save the file.
    This is done via a message box with YES (save and exit workbook), NO (exit workbook), CANCEL (do nothing).

    However, for some reason the message box buttons need to be clicked twice to close the workbook.

    The code I am using is as follows:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim gui As Worksheet
    Dim optimiser As Shape
    
    Set gui = ThisWorkbook.Sheets("GUI")
    Set optimiser = gui.Shapes("Commandbutton2")
    
    Application.DisplayAlerts = False
    
    For Each Sht In Application.Worksheets
        Sht.Sort.SortFields.Clear
    Next Sht
    
    gui.Shapes("Check Box 157").OLEFormat.Object.Value = -4146
    gui.Shapes("Check Box 88").OLEFormat.Object.Value = -4146
    
    Call Exit_Program
    
    
    End Sub
    Sub Exit_Program()
    
    Dim Answer As String
    Dim Question As String
    Question = "All analysis input data will be lost. Do you want to save your file before you exit?"
    Answer = MsgBox(Question, vbYesNoCancel)
    
    
    If Not ThisWorkbook.Saved Then
    Select Case Answer
    Case vbYes
    ThisWorkbook.Save
    Case vbNo
    ThisWorkbook.Saved = True
    Case vbCancel
    Cancel = True
    Exit Sub
    End Select
    End If
    
    
    ThisWorkbook.Close
    
    End Sub
    What am I doing wrong?

    Regards,
    Martin

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Macro to close and save file with message box - error button to be clicked twice

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        
        Dim gui       As Worksheet
        Dim optimiser As Shape
        
        Set gui = ThisWorkbook.Sheets("GUI")
        Set optimiser = gui.Shapes("Commandbutton2")
        
        Application.DisplayAlerts = False
        
        For Each Sht In Application.Worksheets
            Sht.Sort.SortFields.Clear
        Next Sht
        
        gui.Shapes("Check Box 157").OLEFormat.Object.Value = -4146
        gui.Shapes("Check Box 88").OLEFormat.Object.Value = -4146
        
        
        If Not ThisWorkbook.Saved Then
            Select Case MsgBox("Do you want to save your file before you exit?", _
                               vbYesNoCancel, "All analysis input data will be lost.")
                Case vbYes
                    ThisWorkbook.Save
                Case vbNo
                    ThisWorkbook.Saved = True
                Case vbCancel
                    Cancel = True
            End Select
        End If
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ 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. Excel Not Responding after running Save and Close Macro Button
    By mangesh.mehendale in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-25-2016, 08:25 AM
  2. Macro to turn off save box when user click close button
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 03-22-2016, 07:43 AM
  3. Run-time error 70 when button is clicked to run macro that opens email
    By blacklotus0014 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-02-2013, 09:54 AM
  4. [SOLVED] Click button to save as file name --> open message if range is 1
    By debbiesh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-04-2012, 03:05 PM
  5. Determine if the close button in excel 2007 is clicked
    By Hudas in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-17-2012, 10:43 AM
  6. [SOLVED] How to disable save message when I click the cross to close the file
    By hon123456 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2006, 02:20 AM
  7. Replies: 3
    Last Post: 08-12-2005, 03:05 PM

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