+ Reply to Thread
Results 1 to 8 of 8

Workbook_BeforeClose

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016
    Posts
    397

    Workbook_BeforeClose

    I have a workbook with

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Application.DisplayAlerts = False
    With ThisWorkbook
        If MsgBox("Want to save this workbook called " & .Name & "?", vbYesNo) = vbNo Then
            Cancel = True
        Else
            Call EnableEdit(False)
            .Save
            Call ApplicationSettings(True)
        End If
    End With
    Application.DisplayAlerts = True
    
    End Sub
    Pressing the "X", workbook saves if "Yes" is pressed. Pressing "No" does nothing, so no way of closing workbook without saving

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,085

    Re: Workbook_BeforeClose

    so no way of closing workbook without saving
    I think that was the idea, to ensure that the sheets were hidden?

  3. #3
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016
    Posts
    397

    Re: Workbook_BeforeClose

    This is obviously all of the code that has been added to the other workbook i have been working on.

    In this instance, is there any way of allowing this workbook to be closed without the need to save? The only data that is added is the date and the number of staff on roster for the given day - so no issue if they are not recorded.

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,085

    Re: Workbook_BeforeClose

    ok. Are you able to post the workbook again please, just so I look at the right one.

  5. #5
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016
    Posts
    397

    Re: Workbook_BeforeClose

    Quote Originally Posted by ByteMarks View Post
    Are you able to post the workbook again please.
    Attached
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016
    Posts
    397

    Re: Workbook_BeforeClose

    whatever/wherever the issue is, the same happens on this similar workbook - but I do not get the double save prompt.
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,255

    Re: Workbook_BeforeClose

    In order to understand how the whole code structure works, I recommend uncommenting all the Stops and going through the code in step mode.
    All the changed code in the ThisWorkbook module. The rest remains unchanged.:
    Option Explicit
    
    Const WelcomePage$ = "Introduction"
    
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Dim Answer As VbMsgBoxResult
     
    'Stop
      'custom save support when closing the workbook
      If Me.Saved = False Then
        Answer = MsgBox("Want to save your changes to '" & Me.Name & "'?" & Space(20), _
                      vbYesNoCancel + vbExclamation + vbDefaultButton1)
        
        Select Case Answer
          Case vbYes
            Call SaveMe
          Case vbNo
            Me.Saved = True
          Case vbCancel
            Cancel = True
        End Select
       
      End If
      
    End Sub
    
    
    Private Sub Workbook_Open()
    'Stop
        Application.ScreenUpdating = False
        Call ApplicationSettings(False)
        Call EnableEdit(True)
        Application.ScreenUpdating = True
        ThisWorkbook.Saved = True
    End Sub
    
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Dim ws As Object
        Dim wsActive As Object
        Dim vFilename As Variant
    'Stop
        'Turn off screen flashing
       Application.ScreenUpdating = False
        'Record active sheet
        Set wsActive = ActiveSheet
    
        'Save workbook directly or prompt for saveas filename
        If SaveAsUI Then
            vFilename = Application.GetSaveAsFilename( _
                        fileFilter:="Excel Files (*.xls*), *.xls*")
            If TypeName(vFilename) = "Boolean" Then
    'Stop
            Else
                'Save the workbook using the supplied filename
                Application.EnableEvents = False
                
                Call EnableEdit(False)
                
                ThisWorkbook.SaveAs vFilename
                Application.RecentFiles.Add vFilename
                
                Call EnableEdit(True)
                
                wsActive.Select
                Me.Saved = True
                
                Application.EnableEvents = True
            End If
        Else
            Call SaveMe
        End If
    'Stop
        Cancel = True
    End Sub
    
    
    Private Sub SaveMe()
        Dim Sh As Object
    'Stop
    
        Set Sh = ActiveSheet
        
        Application.EnableEvents = False
        
        Call EnableEdit(False)
        Me.Save
        Call EnableEdit(True)
        
        Sh.Select
        Me.Saved = True
        
        Application.EnableEvents = True
    End Sub
    
    
    Sub EnableEdit(blnEnable As Boolean)
        Dim ws As Object
        Dim a
        Dim i As Integer
    'Stop
        
        a = Array(WelcomePage)
    
        With ThisWorkbook
            On Error Resume Next
            i = .Sheets(a).Count
            On Error GoTo 0
            If i <> UBound(a) + 1 Then Exit Sub
    
            Application.ScreenUpdating = False
            Call fileProtection(False)
    
            For i = 0 To UBound(a)
                .Sheets(a(i)).Visible = xlSheetVisible
            Next
    
            For Each ws In .Sheets
                If IsError(Application.Match(ws.Name, a, 0)) Then
                    ws.Visible = IIf(blnEnable, xlSheetVisible, xlSheetVeryHidden)
                End If
            Next
            
            For i = 0 To UBound(a)
                .Sheets(a(i)).Visible = IIf(blnEnable, xlSheetVeryHidden, xlSheetVisible)
            Next
            
            Call fileProtection(True)
            Application.ScreenUpdating = True
        End With
        
    End Sub
    Artik

  8. #8
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016
    Posts
    397

    Re: Workbook_BeforeClose

    Hi Artik.

    That works perfectly well, thank you.

    Just need to look into transferring this over to my other books.

+ 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. Workbook_BeforeClose?
    By kartune85 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-26-2006, 03:55 AM
  2. [SOLVED] Workbook_BeforeClose
    By Alan McQuaid via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2006, 11:40 AM
  3. [SOLVED] Workbook_BeforeClose
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-11-2006, 06:00 AM
  4. workbook_beforeClose
    By GB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2006, 08:45 PM
  5. [SOLVED] Workbook_BeforeClose
    By Andrzej in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-12-2005, 05:05 PM
  6. Personal.xls & Workbook_BeforeClose
    By William in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2005, 02:07 AM
  7. Workbook_BeforeClose problem
    By broogle in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2005, 09:06 AM
  8. Workbook_BeforeClose
    By PO in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2005, 02:06 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