Aug 21 2022 01:21 PM
Aug 21 2022 01:30 PMSolution
You must always call Dir first with arguments. This will return the name of the first file (or folder) that matches the specification.
Each time you call Dir without arguments after that, it will return the name of the next file (or folder) that matches the original specification.
So you can use code like this:
Const sFolder = "C:\Excel\" Dim sFile As String ' Get the name of the first Excel workbook in the folder sFile = Dir(sFolder & "*.xlsx") ' Loop through the workbooks until none is found Do While sFile <> "" ' Do something with the file name MsgBox "Found file: " & sFile ' Get the next file name sFile = Dir Loop