I have the code below. It runs, but only counts the first file and then ends. There are 12 files in the directory that should have been written to the Files Processed worksheet and counted.
My understanding is the 'fil = Dir' line should advance to the next file, but it is just returning an empty string ("") instead of the next file.
Any suggestions and help appreciated.
My understanding is the 'fil = Dir' line should advance to the next file, but it is just returning an empty string ("") instead of the next file.
Any suggestions and help appreciated.
Code:
Option Explicit
Sub countfiles()
Dim z As Long, totfiles As Long
Dim WS As Worksheet
Dim fLdr As String, Fil As String, myCurrFile As String, workshtName As String
'define variables
myCurrFile = ThisWorkbook.name
z = 0
totfiles = 0
Workbooks(myCurrFile).Activate
'turn off screen updating while macro is running
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Set WS = Sheets("Files Processed")
fLdr = ActiveWorkbook.Path
If Right(fLdr, 1) <> "\" Then fLdr = fLdr & "\"
ChDir fLdr
Fil = Dir("*.csv")
Do While Fil <> ""
z = z + 1
WS.Cells(z + 1, 1).Resize(, 4) = _
Array(Dir(Fil), _
FileLen(Fil) / 1000, _
FileDateTime(Fil), _
fLdr)
totfiles = totfiles + 1
Fil = Dir
Loop
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
.StatusBar = False
End With
End Sub