Process One Workbook at a Time in an Excel VBA Loop
Question details
The user needs an Excel VBA loop to process and close each workbook before opening the next file in a folder.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro loops through files in a folder and calls a processing routine for each workbook.
- Observed behavior
- The macro opens multiple workbooks rather than closing the current workbook before moving to the next one.
Open the macro in Excel's VBA editor with Alt+F11 and identify the folder path, workbook variable, and processing routine. Confirm that the files can be saved and that the routine can receive or otherwise identify the workbook being processed.
Save and Close Each Workbook Before the Next Loop Iteration
Place the save and close actions immediately after the processing call so the loop finishes with one workbook before opening another.
The following example processes .xlsx files. Replace the folder path and ProcessWorkbook call with your own path and processing routine. The routine must accept the workbook variable or be adapted to use it.
In Excel, press Alt+F11 and open the module containing the folder loop. Locate the Workbooks.Open statement and the call that processes the opened workbook.
After the processing call, add wb.Save followed by wb.Close before the loop advances. For example: Sub ProcessFiles() Dim folderPath As String Dim fileName As String Dim wb As Workbook folderPath = "C:\YourFolder\" fileName = Dir(folderPath & "*.xlsx") Do While fileName <> "" Set wb = Workbooks.Open(folderPath & fileName) ProcessWorkbook wb wb.Save wb.Close Set wb = Nothing fileName = Dir() Loop End Sub
Click inside ProcessFiles and press F8 repeatedly. Confirm that the processing routine finishes and wb.Save and wb.Close execute before Workbooks.Open runs for the next file.

Work With Excel-Compatible Files in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing spreadsheet files. It supports common Excel formats for an easier move between office suites, but it does not fix an Excel VBA loop; macro support and compatibility can vary.

Frequently Asked Questions
Where should I put the save and close statements?
In the VBA editor, place wb.Save and wb.Close immediately after the call that processes the workbook and before the loop advances to the next filename.
Why does F8 enter my processing routine before closing the workbook?
F8 steps into a called procedure. Continue pressing F8 until the routine returns to the loop; the save and close statements run after that call finishes.
What if I don't want to save changes to each file?
Remove wb.Save and use wb.Close SaveChanges:=False after the processing call. Any unsaved changes to that workbook will be discarded.
Why might a workbook remain open after the macro stops?
An error or interruption before wb.Close can leave it open. Check the highlighted line in the VBA editor, fix the cause, and add error handling if the workbook must be closed after a failure.




