vba before close save workbook

babycody

Well-known Member
Joined
Jul 8, 2003
Messages
1,395
Is there a way to automatically save a workbook and any changes before it is closed? Thanks all.
 

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.
Add this code to the "ThisWorkbook" code module, and it will automatically save the Workbook before that workbook is closed, without displaying a message to save!

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'ThisWorkbook Module code!

ActiveWorkbook.Save
End Sub
 
Upvote 0
Add this code to the "ThisWorkbook" code module, and it will automatically save the Workbook before that workbook is closed, without displaying a message to save!

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'ThisWorkbook Module code!

ActiveWorkbook.Save
End Sub

I use this for my "personal.xlsb" workbook because I want it to stay invisible and I want the saving to be done automatically. But, since this is rarely the "active" workbook, I modified the code. Instead of "ActiveWorkbook.Save" I use "ThisWorkbook.Save".
That way, no matter what workbook is active, the one that you're putting the code into is the one that will save.
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

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