0

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.

7
  • 1
    The code looks like it would work if you dimensioned your arrays Dim xFolder(1 to 2) and same for the rest. Commented May 24, 2019 at 17:52
  • 1
    You'd also want to make sure you are advancing the output cell .Cells(2 + z, 2).Value = xCount(z) Commented May 24, 2019 at 17:53
  • 1
    Most of your declared variables are arrays. Although longs or ints are used for accessing values within arrays, the type of the data stored in the array is what the array needs to be declared as. For instance, if you're storing strings in xFolder, then it needs to be declared as Dim xFolder() as String
    – Mistella
    Commented May 24, 2019 at 17:54
  • 1
    No, you are populating xFolder with strings ("\\generic path") so you'd need to declare it as the type that you're populating it with Dim xFolder(1 to 2) As String Commented May 24, 2019 at 17:54
  • 1
    So xFolder, xFile, and xPath should all be Strings. Commented May 24, 2019 at 17:56

2 Answers 2

3

Define all the following as String array, in your case you want each array to have 2 elements.

Dim xFolder(1 To 2) As String
Dim xPath(1 To 2) As String
Dim xCount(1 To 2) As Long
Dim xFile(1 To 2) As String
0

Thanks for the answers and explanations surrounding types/arrays. Added the y variable to advance the output columns by 1 each time.

Final working code:

Sub CountFiles()

Dim xFolder(1 To 2) As String
Dim xPath(1 To 2) As String 
Dim xCount(1 To 2) As Long
Dim xFile(1 To 2) As String    
Dim z As Long
Dim y 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

y = z + 1

    With Worksheets("test")
        .Cells(3, y).value = xCount(z)
    End With

y = 0

Next z

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.