How to Save and Close a Specific Excel Workbook with VBA
Question details
The user needs to explicitly save and close a targeted Excel workbook using a VBA macro without accidentally affecting other open workbooks.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running a VBA macro to manage and close multiple open workbooks.
- Observed behavior
- Relying on the ActiveWorkbook property causes the macro to save or close the wrong file if focus shifts. The goal is to explicitly reference the intended workbook in the VBA code.
Ensure you have saved your current VBA project and created backups of your open workbooks, as VBA actions like closing a file without saving cannot be undone.
Use ThisWorkbook to Target the Macro's Container File
This is the safest method when you want to save and close the exact workbook where the VBA macro is currently stored and running.
Using ThisWorkbook ensures that your macro always targets the file containing the code, regardless of which workbook is currently active on your screen. This completely eliminates errors caused by shifting window focus.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
Insert a new module from the Insert menu, or open an existing module where you want to add the code.
Type 'ThisWorkbook.Save' to instruct the macro to save the current state of the workbook containing the code.
Type 'ThisWorkbook.Close' on the next line. If you want to close it without further prompts, use 'ThisWorkbook.Close False' to discard unsaved changes, or 'ThisWorkbook.Close True' to automatically save before closing.

Reference a Specific Workbook by Name
Use this method when your macro is running in one workbook, but you need to save and close an entirely different open workbook.
Write and Run VBA Macros Easily in WPS Office
WPS Spreadsheet offers robust, built-in support for VBA macros, allowing you to automate tasks like saving and closing workbooks seamlessly. It provides a familiar developer environment and executes your code efficiently.
- 1. Download and Install WPS Office: Get the free version of WPS Office from the official website and open WPS Spreadsheet.
- 2. Access the Developer Tab: Navigate to the Developer tab on the top ribbon and click on 'Visual Basic' to launch the VBA Editor.
- 3. Insert Code and Run: Insert a new module, paste your workbook management macro, and click 'Run' to seamlessly save and close your files.

Frequently Asked Questions
Why does my macro close the wrong workbook?
If your VBA code uses 'ActiveWorkbook.Close', it will act on whichever workbook is currently active or in focus. If the macro process switches focus to another file before reaching the close command, the wrong file will be closed. Explicitly naming the workbook or using 'ThisWorkbook' prevents this.
What does 'False' mean in ThisWorkbook.Close False?
The 'False' argument corresponds to the 'SaveChanges' parameter. It tells Excel to close the workbook without saving any additional changes, which effectively bypasses the pop-up prompt asking if you want to save.
Can I save a workbook under a new name using VBA?
Yes. Instead of the standard Save method, you can use 'Workbook.SaveAs'. You will need to provide the new file path and name as an argument, for example: 'ActiveWorkbook.SaveAs "C:\Folder\NewName.xlsx"'.




