The VBA Dir function usually returns an empty string because the path you passed does not exist, the file is hidden or has no matching extension, or the code is running with a working directory you did not expect. Check the exact path with a quick Debug.Print, confirm the file is visible in File Explorer, and make sure you are not calling Dir again before you have read the first result. Once those three things line up, Dir starts returning names as expected.
What You'll Need
- Excel with the Visual Basic Editor enabled (Alt+F11 opens it on Windows; on Mac it is Developer > Visual Basic).
- The exact folder path and file name you are trying to list, copied from File Explorer's address bar rather than typed from memory.
- A blank module where you can paste short test procedures.
- File Explorer set to show hidden items and file name extensions, so you can see what VBA sees.
Step-by-Step
- Print the path you are actually passing. In the Immediate window (Ctrl+G in the VBE), type
? "C:\Your\Folder\"with your real path. If it looks wrong, fix the string in your code before changing anything else. A trailing backslash is required when you pass a folder toDir. - Confirm the folder exists. Paste the same path into File Explorer's address bar. If Explorer cannot open it, VBA cannot either. Watch for OneDrive or SharePoint paths that look local but are actually cloud URLs, and for mapped network drives that are disconnected.
- Test the simplest call. In the Immediate window run
? Dir("C:\Your\Folder\*"). If that returns a name, the folder is fine and the problem is in your pattern or your loop. If it returns nothing, the folder or the pattern is wrong. - Check the file extension.
Dir("C:\Folder\*.xlsx")will not find.xlsmfiles. Use*.*while testing, then narrow down once you know files are being found. - Show hidden and system files. In File Explorer, open View and tick Hidden items and File name extensions.
Dirskips hidden files unless you pass thevbHiddenattribute, so a file that is invisible in Explorer is also invisible to a plainDircall. - Check for a second
Dircall too early.Dirkeeps internal state. The first call with arguments starts a new search; later calls with no arguments return the next match. If you callDir("C:\Folder\*")twice in a row, you restart the search and lose the first result. Store the first result in a variable, then loop with bareDir. - Use the correct loop shape. A working pattern is:
fileName = Dir(path & "*.*"), thenDo While fileName <> "", process the file, thenfileName = Dirat the end of the loop body. Forgetting the final bareDirgives you an infinite loop on the first file. - Watch for long paths and special characters. Paths over roughly 260 characters, or names containing characters like
#or&, can fail. Move the test folder to something short such asC:\Testto isolate the issue. - Check your working directory assumption. A relative path like
Dir("*.xlsx")uses the current directory, which is not always the folder the workbook lives in. Always build an absolute path withThisWorkbook.Pathor a named folder variable. - Confirm macro security is not blocking file access. If the workbook is in a Trusted Location and macros are enabled, file reads work normally. If the file is opened from the internet, Windows may block it; right-click the file in Explorer, open Properties, and clear the block if it is present.
Common Mistakes
- Calling
Dirwith arguments inside the loop, which restarts the search every time and returns the same first file forever. - Omitting the trailing backslash, so
C:\Folder*is treated as a partial name rather than a folder plus wildcard. - Assuming
Dirreturns full paths. It returns only the file name; join it to the folder yourself. - Testing against a folder that is syncing through OneDrive and not yet downloaded locally.
- Using
Diron a Mac path with Windows-style backslashes. On Mac, use the colon-separated path orMacScript-free alternatives, and test on the target platform.
Tips
- Keep a tiny test sub that only prints results to the Immediate window. It isolates
Dirproblems from the rest of your macro. - Use
Dir(path, vbDirectory)when you need folder names as well as files, and skip the.and..entries that come back. - Build paths with
Application.PathSeparatorso the same code behaves on Windows and Mac. - Prefer
FileSystemObjectwhen you need attributes, sizes or dates;Dironly gives names. - Add
Option Explicitat the top of the module so a misspelled variable does not silently break your loop.
Troubleshooting
If Dir still returns an empty string, work backwards. First, run ? Dir("C:\*") on a folder you know exists; if that works, the problem is your specific folder. Second, copy the path from Explorer's address bar directly into the Immediate window to rule out typos and invisible characters. Third, check whether the folder is on a network share that requires sign-in; a disconnected mapped drive returns nothing. Fourth, test with a file you created yourself in the same folder to rule out permissions. If the macro runs from a different workbook, confirm ThisWorkbook.Path is not empty (unsaved workbooks have no path). Finally, close and reopen Excel and try again; a stale VBA state after an error can leave Dir mid-search, and a fresh session clears it.
Frequently Asked Questions
Why does Dir return an empty string even though the file exists?
Most often the path is wrong, the file is hidden, or the extension does not match your pattern. Test with *.* and show hidden files in Explorer.
Does Dir return the full path?
No. It returns only the file or folder name. Combine it with the folder path yourself.
Why does my Dir loop keep returning the same file?
You are probably calling Dir with arguments inside the loop. Use a bare Dir at the end of the loop body to advance to the next match.
Can Dir find files in subfolders?
Not directly. It lists one folder at a time. Call it again for each subfolder, or use FileSystemObject for recursive searches.
Does Dir work on Mac Excel?
Yes, but the path format differs. Use colon-separated paths or Application.PathSeparator and test on the Mac itself.
Why does Dir fail on a OneDrive folder?
The file may not be downloaded locally, or the path may point to a cloud URL rather than a local folder. Open the folder in Explorer first and copy the local path.