VBA & Macro Problems

How to Save and Close a Specific Excel Workbook with VBA

Jason MillerJason Miller Nov 3, 2026 868 views

Question details

The user needs to explicitly save and close a targeted Excel workbook using a VBA macro without accidentally affecting other open workbooks.

How to Save and Close a Specific Excel Workbook with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Create or Edit a Macro

Insert a new module from the Insert menu, or open an existing module where you want to add the code.

3
Add the Save Command

Type 'ThisWorkbook.Save' to instruct the macro to save the current state of the workbook containing the code.

4
Add the Close Command

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.

Use ThisWorkbook to Target the Macro's Container File
Best Practice: Combining both actions into a single line like 'ThisWorkbook.Close SaveChanges:=True' is the most efficient way to safely save and exit the file.
Enhance Productivity with WPS Spreadsheet

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. 1. Download and Install WPS Office: Get the free version of WPS Office from the official website and open WPS Spreadsheet.
  2. 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. 3. Insert Code and Run: Insert a new module, paste your workbook management macro, and click 'Run' to seamlessly save and close your files.
Full support for standard Excel VBA syntax and workbook objects.High compatibility with Microsoft Excel macro-enabled formats (.xlsm, .xls).Lightweight performance ensures faster macro execution without freezing.Familiar Developer tab and Visual Basic Editor interface.
microsoft office alternative - wps office

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"'.