VBA & Macro Problems

How to Loop Through, Update, Save, and Close Excel Files Using VBA

Algirdas JasaitisAlgirdas Jasaitis Nov 2, 2026 869 views

Question details

The user needs a VBA procedure to process multiple macro-enabled workbooks in a specific folder by running a macro in each file, saving changes, and closing them sequentially.

How to Create a VBA Loop to Open, Update, Save, and Close Excel Files
Product
Excel
Device & OS
not provided
Scenario
Automating repetitive data updates across multiple macro-enabled workbooks stored in a single directory.
Observed behavior
To successfully process the files without errors, the loop must use correct file extension patterns, exclude the master workbook, and explicitly pass the workbook object to the update procedure.
Before you start

Verify that the target folder contains only the specific macro-enabled workbooks (.xlsm) you intend to modify, and ensure the master workbook containing this code is backed up.

Solution 1Recommended

Build a VBA Loop with Precise Object Passing and Filtering

Use the Dir function with strict file patterns to loop through the folder, explicitly assign workbook objects, and pass those objects to your update procedure.

A common point of failure when looping through files in VBA is relying on ActiveWorkbook or using vague file patterns that pick up hidden temporary files. Explicitly assigning the workbook to a variable ensures that the update macro modifies the correct file.

1
Define the target folder and file pattern

Set your folder path variable and use a specific file extension pattern like "*.xlsm". Avoid wildcards like "*.xlsm*" to prevent the macro from attempting to open system temporary files.

2
Start the Dir loop

Use the Dir() function to find the first file, and initiate a 'Do While' loop that continues as long as the filename string is not empty.

3
Filter out the master workbook

Inside the loop, insert an 'If' statement to check that the current file name does not match ThisWorkbook.Name. This prevents the macro from trying to update or close the file that is actively running the script.

4
Open and assign the workbook

Use 'Set wb = Workbooks.Open(folderPath & fileName)' to open the target file and assign it directly to a Workbook variable.

5
Pass the object to the update procedure

Call your update macro (e.g., ForecastUpdate) and pass the 'wb' object to it as an argument, so the procedure knows exactly which workbook to target.

6
Save, close, and iterate

Execute 'wb.Close SaveChanges:=True' to save the modifications and close the file. Then, use 'fileName = Dir()' to fetch the next file in the directory.

Build a VBA Loop with Precise Object Passing and Filtering
Variable Passing: By passing the 'wb' object directly to your update subroutine, you guarantee that the changes are applied to the correct file, completely eliminating ActiveWorkbook focus errors.
Advanced Spreadsheet Automation

Automate Batch File Processing with WPS Spreadsheet Macros

WPS Office offers comprehensive VBA support, allowing you to easily write, run, and debug loops to update multiple Excel files simultaneously with excellent performance.

  1. 1. Enable the Developer Tab: Open WPS Spreadsheet, navigate to the Options menu, and ensure the Developer tab is enabled on your ribbon.
  2. 2. Open the VBA Editor: Click on 'Visual Basic' or 'Macro' in the Developer tab to launch the integrated coding environment.
  3. 3. Insert the Looping Code: Insert a new Module and paste your Dir loop script, ensuring your file paths are properly formatted.
  4. 4. Run and Automate: Run the macro to instantly process, update, save, and close all designated Excel files in your folder.
Fully compatible with Microsoft Excel .xlsm and .xlsb formats.Built-in robust VBA Editor for seamless macro troubleshooting.Fast execution when batch processing dozens of workbooks.Lightweight, free, and designed for maximum productivity.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA loop crashing when it hits temporary files?

Using a broad file pattern like "*.xlsm*" can accidentally match hidden temporary Excel files (which typically start with "~$"). Ensure your code uses the exact extension pattern "*.xlsm".

How do I prevent the macro from closing the workbook containing the code?

Inside your loop, immediately after obtaining the file name, use a conditional statement like 'If fileName <> ThisWorkbook.Name Then' to skip processing the master workbook.

Why does my update macro modify the wrong workbook during the loop?

This happens when the update subroutine relies on 'ActiveWorkbook', which can lose focus. Explicitly pass the opened workbook object variable (e.g., 'wb') to your update subroutine so it only modifies that specific file.