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.
What code do I need to use so that code runs only once?
Biz
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: