Saving Changes

You can close a workbook using the Close method of the Workbook object, as shown here: ActiveWorkbook.Close

If changes have been made to the workbook, the user will be prompted to save the changes when an attempt is made to close the workbook. If you want to avoid this prompt, you can use several techniques, depending on whether or not you want to save the changes.

If you want to save changes automatically, you can specify this as a parameter of the Close method:

Sub CloseWorkbook()

Dim wkbl As Workbook

Set wkbl = Workbooks.Open(FileName:="C:\Data\SalesData1.xlsx") Range("A1").Value = Format(Date, "ddd mmm dd, yyyy") Range("A1").EntireColumn.AutoFit wkbl.Close SaveChanges:=True End Sub

If you don't want to save changes, you can set the SaveChanges parameter of the Close method to False.

Another situation that could arise is where you want to leave a changed workbook open to view, but you don't want to save those changes or be prompted to save the changes when you close the workbook or Excel. In this situation, you can set the Saved property of the workbook to True and Excel will think that there are no changes to be saved. You should make doubly sure you would want to do this before you add this line of code:

ActiveWorkbook.Saved = True

0 0

Post a comment