How to Fix a VBA Do While Loop That Processes Only One File
Question details
The user is attempting to run a VBA loop using the Dir function to enumerate through multiple files in a folder, but the macro either stops after one file or gets stuck in an infinite loop.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating file processing across multiple files in a specific directory using a Do While loop in VBA.
- Observed behavior
- The macro stops after processing the first file, skips subsequent files, or freezes completely because the next file is not fetched.
Make sure you have a backup copy of your macro script and ensure the directory you are testing contains the correct target files.
Move the Dir() Call Outside the Conditional Block
Relocating the filePath = Dir() assignment ensures that the loop continually fetches the next file, preventing infinite loops.
When processing files with the Dir function, you must call Dir() repeatedly to get the next file in the folder. If you place this call inside an If block and the condition evaluates to false, VBA skips the Dir() call. Consequently, the file path never updates, and your Do While loop becomes trapped in an infinite loop processing the same file.
Press ALT + F11 to open the VBA editor and locate your specific subroutine.
Find the line of code that reads 'filePath = Dir()' within your Do While loop.
Move 'filePath = Dir()' so it sits completely outside of any If...End If statements. The best place is usually right before the 'Loop' command at the bottom of the loop.

Adjust Hardcoded Loop Counters
Check your macro for any loop limits that intentionally stop the script from processing additional files.
Debug and Run VBA Macros Easily with WPS Spreadsheet
WPS Office provides a comprehensive built-in Macro Editor, making it incredibly easy to write, edit, and debug your VBA loops. Whether you are automating reports or processing multiple files via the Dir function, WPS Spreadsheet handles your scripts seamlessly.
- 1. Open Your File in WPS: Launch WPS Spreadsheet and open your macro-enabled workbook (.xlsm).
- 2. Access the VBA Editor: Navigate to the 'Tools' tab on the ribbon and click on 'Macro' to open the VBA Editor interface.
- 3. Step Through the Loop: Use the F8 key to run your Do While loop line by line. This allows you to monitor the 'filePath' variable and ensure the Dir() function fetches the next file correctly.

Frequently Asked Questions
Why does my VBA Do While loop freeze my application?
A VBA loop typically freezes and causes the application to hang if it encounters an infinite loop. When using the Dir function, this usually happens if the Dir() call meant to fetch the next file is bypassed or placed inside an unmet If condition, causing the loop to check the same file path indefinitely.
How do I loop through all files in a folder without knowing how many there are?
You can process an unknown number of files by initializing the Dir function with your folder path, and then setting your loop condition to 'Do While filePath <> ""'. Ensure you call 'filePath = Dir()' at the end of every loop iteration so it continues reading until the folder is empty.
Can I run standard Excel VBA Macros in WPS Office?
Yes, WPS Spreadsheet offers outstanding compatibility with Microsoft Excel VBA macros. You can open your existing .xlsm files and execute your automated loops, including Dir functions and custom conditional logic, using the integrated WPS Macro Editor.




