[wpfilebase tag=file path='01/SaveNoSave.zip']
The entry is a simple example of how to close a workbook and either force a save, or close without saving.
These are one line entries intended to be used with a command button, menu bar button or in another procedure.
There are always instances where you will want to force your users to save a workbook upon exiting, or exit without saving anything. These lines of code are intended to demonstrate how to do this, and would most likely be assigned to a command button, menu bar button, or entered into another procedure.
NOTE: The CloseNoSave will not prohibit a user from saving the workbook through other means.
Sub CloseNoSave() 'Close the workbook without saving it ThisWorkbook.Close savechanges:=False End Sub
Sub CloseForceSave() 'Save the workbook, then close it ThisWorkbook.Close savechanges:=True End Sub
How to use:
- Copy above code.
- In Excel press Alt + F11 to enter the VBE.
- Press Ctrl + R to show the Project Explorer.
- Right-click desired file on left (in bold).
- Choose Insert -> Module.
- Paste code into the right pane.
- Press Alt + Q to close the VBE.
- Save workbook before any other changes.
Test the code:
- Enter some data on the worksheet.
- Press Alt + F8 to bring up the macro dialog box.
- Choose a macro to run.
- When the workbook closes, reopen it.
- If you chose CloseNoSave, the changes you made in step 1 will not have been saved. If you chose CloseForceSave, the changes you made in step 1 will have been saved.
SOURCE | LINK (Vbaexpress.com) | LANGUAGE | ENGLISH |