Sub Workbook_BeforeClose

Img

New Member
Joined
Jan 7, 2009
Messages
7
Hi there

This piece of code runs correctly

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim XCount As Integer
XCount = Application.Sheets.Count
Revised = Format(Now(), "mmm dd, yyyy")

Dim i As Integer
Dim Str As String
i = 1
Str = Sheets(i).Name

Dim Msg, Style, Title, Response
Msg = "Is this a revision from the previous version? If Yes, Last Revised Date will be changed to today's date." ' Define message.
Style = vbYesNo
Title = "Last Revision" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes.
Sheets(Str).Range("L8").Value = Revised
MsgBox ("The Last Revised Date is now changed to " & Revised)

Else
MsgBox ("GoodBye")
End If
If XCount > 1 Then

For i = i To XCount
Str = Sheets(i).Name
Sheets(Str).Range("L8").Value = Revised
Next i
End If

End Sub

However, the user gets a dialog box to ask whether to save the changes.
which obviously we want to if the revision date is inserted.
But the annoying thing is if I insert before the End Sub line

ActiveWorkbook.Save
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True

It runs the Message Box again
"Is this a revision from the previous version? If Yes, Last Revised Date will be changed to today's date."

I have been working all day to get rid of this second time popping up of the Message Box but with no success.

Can you please help me??

Gratefully
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hello and welcome to MrExcel.

Try

Code:
ActiveWorkbook.Save
Application.DisplayAlerts = False
Application.EnableEvents = False
ActiveWorkbook.Close
Application.EnableEvents = True
Application.DisplayAlerts = True
 
Upvote 0
The BeforeClose event will fire again when your code closes the workbook. Try:

Code:
ActiveWorkbook.Save
Application.DisplayAlerts = False
Application.EnableEvents = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
Application.EnableEvents = True
 
Upvote 0
Thank You!!

It took you 2 seconds but me all day!!
So I need to turn off the Enable event, ah..... :))

Thank you again.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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