Trying to set the variable names in a way that allows looping the code without having to type out an instance of each 'directory'.
The intent of this code is to count how many files are in each directory.
Code I have so far (doesn't work):
Sub CountFiles()
Dim xFolder() As Long
Dim xPath() As Long
Dim xCount() As Long
Dim xFile() As String
Dim z As Long
xFolder(1) = "\\generic path"
xFolder(2) = "\\generic path2"
For z = 1 To 2
xPath(z) = xFolder(z) & "\*.xlsx"
xFile(z) = Dir(xPath(z))
Do While xFile(z) <> ""
xCount(z) = xCount(z) + 1
xFile(z) = Dir()
Loop
With Worksheets("test")
.Cells(3, 2).value = xCount(z)
End With
Next z
End Sub
If I don't use the looping method, I can just set the variables to be xFolder1
, xFolder2
, xFolder3
etc., but then I'd have to run an instance of the code which loops through the directories to count for each iteration.
Is there a way to do this? Thanks.
Dim xFolder(1 to 2)
and same for the rest..Cells(2 + z, 2).Value = xCount(z)
xFolder
, then it needs to be declared asDim xFolder() as String
xFolder
with strings ("\\generic path"
) so you'd need to declare it as the type that you're populating it withDim xFolder(1 to 2) As String