How to Loop Through, Update, Save, and Close Excel Files Using VBA
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.

- 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.
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.
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.
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.
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.
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.
Use 'Set wb = Workbooks.Open(folderPath & fileName)' to open the target file and assign it directly to a Workbook variable.
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.
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.

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. Enable the Developer Tab: Open WPS Spreadsheet, navigate to the Options menu, and ensure the Developer tab is enabled on your ribbon.
- 2. Open the VBA Editor: Click on 'Visual Basic' or 'Macro' in the Developer tab to launch the integrated coding environment.
- 3. Insert the Looping Code: Insert a new Module and paste your Dir loop script, ensuring your file paths are properly formatted.
- 4. Run and Automate: Run the macro to instantly process, update, save, and close all designated Excel files in your folder.

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.




