Workbook_BeforeClose

AD_Taylor

Well-known Member
Joined
May 19, 2011
Messages
687
I have a workbook that uses the =TODAY function in some of the cells and due to this it always asks me if I want to save changes when I open it on a different day.

I want to add a Workbook Before Close Event to it that so that when I press close it will automatically save before it then closes down.

I currently have this:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

ActiveWorkbook.Save

End Sub
Couple of questions:

1) What would happen with this code if I used it in a new workbook that hadn't been previously saved?
1a) Can I check to see if it was previously saved or not?
2) Are there any negatives that can be thought of by using code like this? For this workbook I will always want to save it, there is no question of that.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
1. You would be prompted for a file name with the File Save dialog.

1a. Try this

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

If Not Me.Saved Then Me.Save

End Sub
2. If you made some catastrophic error that would be saved automatically. You could turn off events in the Immediate Window (CTRL + G)

Application.EnableEvents=False

then close the file to prevent it being saved.
 
Upvote 0
You can use this...
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

ThisWorkbook.Save

End Sub

1.If you use it to a new workboko then it will ask you if you want to save it.
1a. I think if the workbook closes without asking you to save then it was previouly saved otherwise not.
2.I guess the negative is when your PC hangs up and you dont want actually to sve anything it will automatically save aso when accidentally closed it that supposed not to be saved.
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,147
Members
452,891
Latest member
JUSTOUTOFMYREACH

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