Workbook_BeforeClose not working

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Dear All,

I am trying to close file and based on user selection, either save file or does not save the file. When vba is run it automatically runs twice which is error if user has selected either Yes, No, Cancel.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim iReply As Byte, iType As Integer
'<ANY code>
Application.DisplayAlerts = False
Application.ScreenUpdating = False
    ' Define buttons argument.
      iType = vbYesNoCancel + vbCritical + vbDefaultButton2
        iReply = MsgBox("Would you like to save now?", _
                        iType)
Select Case iReply
Case Is = vbYes         ' user chose Yes save current workbook
    ThisWorkbook.Close savechanges:=True
Case Is = vbNo           ' user chose No, don't save
    ThisWorkbook.Close savechanges:=False
Case Is = vbCancel      ' user chose Cancel, it stops the code from running
    Exit Sub
End Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

What code do I need to use so that code runs only once?

Biz
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
An untested guess

Rich (BB code):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim iReply As Byte, iType As Integer
'
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
    ' Define buttons argument.
      iType = vbYesNoCancel + vbCritical + vbDefaultButton2
        iReply = MsgBox("Would you like to save now?", _
                        iType)
Select Case iReply
Case Is = vbYes         ' user chose Yes save current workbook
    ThisWorkbook.Close savechanges:=True
Case Is = vbNo           ' user chose No, don't save
    ThisWorkbook.Close savechanges:=False
End Select
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Case for cancel removed, if it's not yes of no then it nust be cancel so do nothing.

Your method of "Exit Sub" was leaving screenupdating and alerts set to false, and would have done the same with events, meaning if the user cancels, the code would not fire next time they try to close.
 
Last edited:
Upvote 0
Hi Jasonb75,

Workbook_BeforeClose not firing and msgbox is not appearing.

Biz
 
Upvote 0
Kill excel and restart it, or type Application.EnableEvents=True in the Immediete window and press Enter. You may have left events turned off.

Rather than killing events in BeforeClose, you might want to set a flag:
Rich (BB code):
Option Explicit
    
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim iReply As Byte, iType As Integer
Static bolInProcess As Boolean
    
    If Not bolInProcess Then
        
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        bolInProcess = True
            ' Define buttons argument.
              iType = vbYesNoCancel + vbCritical + vbDefaultButton2
                iReply = MsgBox("Would you like to save now?", _
                                iType)
        Select Case iReply
        Case vbYes         ' user chose Yes save current workbook
            ThisWorkbook.Save
            Application.DisplayAlerts = True
            Application.ScreenUpdating = True
            ThisWorkbook.Close savechanges:=False
        Case vbNo, vbCancel          ' user chose No, don't save
            Application.DisplayAlerts = True
            Application.ScreenUpdating = True
            ThisWorkbook.Close savechanges:=False
        End Select
    End If
End Sub

The Static will retain the flag's value and stop the second loop, but the flag dies harmlessly as the book closes, preventing accidently leaving events shut off.
 
Upvote 0
No need to explicitly close, since you're already closing:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
   Dim iReply As Byte, iType As Integer
   Application.DisplayAlerts = False
   Application.ScreenUpdating = False
   ' Define buttons argument.
   iType = vbYesNoCancel + vbCritical + vbDefaultButton2
   iReply = MsgBox("Would you like to save now?", _
                   iType)
   Select Case iReply
      Case Is = vbYes         ' user chose Yes save current workbook
         ThisWorkbook.Save
      Case Is = vbNo           ' user chose No, don't save
   End Select
   Application.DisplayAlerts = True
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
No need to explicitly close, since you're already closing:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
   Dim iReply As Byte, iType As Integer
   Application.DisplayAlerts = False
   Application.ScreenUpdating = False
   ' Define buttons argument.
   iType = vbYesNoCancel + vbCritical + vbDefaultButton2
   iReply = MsgBox("Would you like to save now?", _
                   iType)
   Select Case iReply
      Case Is = vbYes         ' user chose Yes save current workbook
         ThisWorkbook.Save
      Case Is = vbNo           ' user chose No, don't save
   End Select
   Application.DisplayAlerts = True
   Application.ScreenUpdating = True
End Sub

Hi Rorya,

Your code works wells but if a person click on Cancel then workbook should not close. It means current work should still be open as user changed his/her mind closing workbook.

Is there a way to fix this problem?

Biz
 
Upvote 0
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
   Dim iReply As Byte, iType As Integer
   Application.DisplayAlerts = False
   Application.ScreenUpdating = False
   ' Define buttons argument.
   iType = vbYesNoCancel + vbCritical + vbDefaultButton2
   iReply = MsgBox("Would you like to save now?", _
                   iType)
   Select Case iReply
      Case Is = vbYes         ' user chose Yes save current workbook
         ThisWorkbook.Save
      Case Is = vbNo           ' user chose No, don't save
      Case is = vbcancel
           Cancel = true
End Select
   Application.DisplayAlerts = True
   Application.ScreenUpdating = True
End Sub

Should do it.
 
Upvote 0
Hi rorya,

It works. Thank you very much mate.

Biz
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,919
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top