Close Method

Applies To

Window Object, Workbook Object, Workbooks Collection.

Description

Closes the object. The Workbooks collection uses Syntax 1. Window and Workbook objects use Syntax 2.

Syntax 1

object.Close

Syntax 2

object.Close(saveChanges, fileName, routeWorkbook)

object

Required. The object to close.

saveChanges

Optional. If there are no changes to the workbook in the window, this argument is ignored. If there are changes to the workbook, and there are other windows open on the workbook, this argument is ignored. If there are changes to the workbook, and there are no other windows open on the workbook, this argument takes the specified action, as shown in the following table.

Value

Action

True

Saves the changes to the workbook. If there is not yet a filename associated with the workbook, then fileName is used. If fileName is omitted, the user is asked to supply a filename.

False

Does not save the changes to this file.

Omitted

Displays a dialog box asking the user whether or not to save changes.


fileName

Optional. Save changes under this filename.

routeWorkbook

Optional. If the workbook does not need to be routed to the next recipient (has no routing slip, or is already routed), this argument is ignored. Otherwise, Microsoft Excel routes the workbook as shown in the following table.

Value

Meaning

True

Sends the workbook to the next recipient.

False

Does not send the workbook.

Omitted

Displays a dialog box asking the user if the workbook should be sent.


Remarks

Closing a workbook from Visual Basic does not run any Auto_Close macros in the workbook. Use the RunAutoMacros method to run the auto close macros.

You cannot close a workbook while a dialog box is displayed. This means that you cannot call this method from an event handler for a dialog-box control. You can use the following steps to close the workbook when the user chooses a button control:

1. Set the DismissButton property for the button to True.

2. Create a module-level status variable

3. Set the status variable to 0 (zero) before you display the dialog box

4. Set the status variable to 1 when the user chooses the control. Choosing the control dismisses the dialog box.

5. Test the status variable. If it is equal to 1, close the workbook.

Example

This example closes BOOK1.XLS and discards any changes.


Workbooks("BOOK1.XLS").Close saveChanges:=False

This example closes all open workbooks. If there are changes in any workbook, Microsoft Excel displays the appropriate prompts and dialog boxes to save changes.


Workbooks.Close