closing Excel with automatic save

April 29, 2011 at 05:42:42
Specs: Windows 7
I am designing an inventory sheet in Excel that has a unique document number which increases every time I print. I want the user to never close the file without saving first. Is there a VB code or macro that I can add that will always save the file when it is closed and not allow the user to close without saving? Thanks you for your help.

Terry


See More: closing Excel with automatic save

Report •

#1
April 29, 2011 at 10:07:20
Use Alt-F11 to open the VBA editor.

Double click the ThisWorkbook module.

Paste the code below into the pane that opens.

Close the workbook.

As always, I suggest that you try this code in a backup copy of your workbook. Nothing you get from a stranger in a forum should be considered safe.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Me.Save
End Sub

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.


Report •

#2
April 29, 2011 at 15:21:10
Auto save features can cause trouble in some cases. Personally there will be times I open a file and manipulate data with no intention of saving the file; so if this feature were present without the end-users knowledge you may find that the files data could become corrupt. Something to keep in mind.


Report •

#3
April 29, 2011 at 16:02:38
While everything you say about "auto save" features is true, it's my hope that along with adding this feature the OP has also safeguarded the workbook with data validation, locked cells, protected sheets, etc.

If the OP is concerned with making sure that the file has been saved every time before it is closed, I can only hope he has taken the necessary precautions to ensure that what gets saved is only what should be saved.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.


Report •

Related Solutions

#4
April 30, 2011 at 05:59:17
Thanks so much for the code. It works great!!

Report •

Ask Question