Excel VBA Dir Function Not Working? Fix It Fast
In this article
The Excel VBA Dir function usually fails for one of four reasons: the path is wrong, the file is hidden or lacks the expected attribute, the folder is not the current directory, or the code is reusing a Dir loop incorrectly. Fix it by testing the exact path with Dir(path) in the Immediate Window, checking for hidden files with Dir(path, vbHidden), and making sure you call Dir with no arguments only inside an active loop. This guide walks through each cause, step by step, so you can get Dir returning file names again.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer).
- Access to the VBA editor (Alt+F11).
- The exact folder path you want to search, including the drive letter.
- Basic familiarity with running a macro or using the Immediate Window (Ctrl+G in the VBA editor).
Step-by-Step
- Confirm the path is exactly right. Open File Explorer, navigate to the folder, click the address bar, and copy the path. In VBA, paste it into a string and test it in the Immediate Window:
? Dir("C:\Users\YourName\Documents\*.xlsx"). If it returns an empty string, the path, wildcard, or file extension is wrong. Note that menu labels and folder names vary by Windows version and region, but the address bar copy method works everywhere. - Check for hidden files. By default,
Dirskips hidden and system files. If the file you expect is hidden, callDir(path, vbHidden)or combine attributes:Dir(path, vbHidden + vbSystem). In File Explorer, turn on View > Hidden items to see whether the file is actually hidden. - Make sure the folder is the current directory.
Dirwith a relative path (for exampleDir("*.xlsx")) searches the current directory, which may not be the workbook's folder. Use a full path, or set the directory first withChDir "C:\Your\Folder". In Excel, the current directory can change when you open or save files, so full paths are safer. - Use the loop correctly. The first call to
Dirtakes the path and pattern. Every subsequent call inside the loop must useDirwith no arguments. Mixing the two, or callingDirwith a new path inside the loop, resets the enumeration and can return the same file repeatedly or stop early. A correct pattern is:fileName = Dir(path), thenDo While fileName <> "", process the file, thenfileName = Dir, andLoop. - Check the file extension and wildcard. If you search for
*.xls, it will not match.xlsxfiles. Use*.*to list everything, or the exact extension. Also confirm there are no stray spaces in the path string. - Test with a known file. Create a test file in a simple folder such as
C:\Temp\test.txtand run? Dir("C:\Temp\test.txt"). If that works, the function is fine and the problem is in your original path or pattern. - Check permissions and network paths. If the folder is on a network share or OneDrive, make sure your account has read access and the path uses the correct UNC format (
\\Server\Share\Folder). Mapped drive letters can fail if the drive is not mapped in the session running the macro. - Look for a missing reference or compile error. In the VBA editor, use Debug > Compile VBAProject. If
Diris flagged, check that you have not declared a variable or function namedDir, which shadows the built-in function.
Common Mistakes
- Calling
Dirwith a path inside a loop instead of the no-argument form, which restarts the search. - Forgetting that
Dirskips hidden and system files unless you pass the attribute constants. - Using a relative path and assuming it points to the workbook's folder.
- Searching for
*.xlswhen the files are.xlsxor.xlsm. - Including a trailing backslash in the path incorrectly, or missing one between folder and filename.
- Naming a variable
Dir, which hides the built-in function.
Tips
- Always test the exact path string in the Immediate Window before writing a loop.
- Use
Dir(path, vbNormal + vbHidden + vbSystem)when you need to see every file. - Store the folder path in a constant or variable and build the search string with
&to avoid typos. - For recursive folder searches,
Diralone cannot recurse; use the FileSystemObject from the Scripting Runtime, or loop through subfolders withDir(path, vbDirectory)and skip.and... - If you only need file names once, collect them into an array or collection before processing, so a later
Dircall elsewhere does not interfere.
Troubleshooting
If Dir still returns an empty string, work from simplest to most involved. First, restart Excel and try the Immediate Window test again. Second, verify the path by pasting it into File Explorer's address bar; if Explorer cannot open it, VBA cannot either. Third, check whether the file is open in another program with an exclusive lock, which can prevent enumeration on some network shares. Fourth, test with a local folder instead of a network or cloud path to isolate permissions. Fifth, if the macro runs from a different workbook, confirm the current directory with ? CurDir and switch to full paths. If you get a compile error, use Debug > Compile VBAProject and rename any variable called Dir. If the loop returns the same file repeatedly, you are almost certainly passing a path to Dir inside the loop; change that call to Dir with no arguments. If nothing works, create a new blank workbook with a single test macro to rule out corruption in the original file.
Frequently Asked Questions
Why does Dir return an empty string for a file I can see in Explorer?
The file is probably hidden or the path or extension does not match. Test with Dir(path, vbHidden) and confirm the full path and extension.
Can Dir search subfolders automatically?
No. Dir lists only the specified folder. To include subfolders, loop with vbDirectory and recurse, or use the FileSystemObject.
Why does my Dir loop keep returning the same file?
You are calling Dir with a path inside the loop. Use Dir with no arguments for every call after the first.
Does Dir work with OneDrive or network paths?
Yes, if the path is correct and your account has read access. Use the full UNC path for network shares and avoid relying on mapped drive letters.
How do I list files with any extension?
Use *.* as the pattern, for example Dir("C:\Folder\*.*").
Can I use Dir to check whether a single file exists?
Yes. If Dir(path) <> "" Then is a common existence check. Remember to include the file extension.