I'm trying to create a two-dimensional loop that loops through a file path that puts the folder name in one column, and then the files in that folder in the second column. The output should look like this:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Folder 1
[/TD]
[TD]File name 1[/TD]
[/TR]
[TR]
[TD]Folder 1[/TD]
[TD]File name 2
[/TD]
[/TR]
[TR]
[TD]Folder 1[/TD]
[TD]File name 3[/TD]
[/TR]
[TR]
[TD]Folder 2[/TD]
[TD]File name 1[/TD]
[/TR]
[TR]
[TD]Folder 3[/TD]
[TD]File name 1[/TD]
[/TR]
[TR]
[TD]Folder 3[/TD]
[TD]File name 2[/TD]
[/TR]
[TR]
[TD]Folder 3[/TD]
[TD]File name 3[/TD]
[/TR]
[TR]
[TD]Folder 3[/TD]
[TD]File name 4[/TD]
[/TR]
[TR]
[TD]Folder 4[/TD]
[TD]File name 1[/TD]
[/TR]
[TR]
[TD]Folder 4[/TD]
[TD]File name 2[/TD]
[/TR]
</tbody>[/TABLE]
Whenever I run my current macro, it's timing out and I'm not sure what's going on. Here's my current code:
If I take out the second dimension that grabs the file names, I can correctly get the macro to grab the folder name and print them in Column A. However, when I add in the second-dimension loop, it times out and my computer is seemingly about to explode. Any help is appreciated. Thank you!
[TABLE="width: 500"]
<tbody>[TR]
[TD]Folder 1
[/TD]
[TD]File name 1[/TD]
[/TR]
[TR]
[TD]Folder 1[/TD]
[TD]File name 2
[/TD]
[/TR]
[TR]
[TD]Folder 1[/TD]
[TD]File name 3[/TD]
[/TR]
[TR]
[TD]Folder 2[/TD]
[TD]File name 1[/TD]
[/TR]
[TR]
[TD]Folder 3[/TD]
[TD]File name 1[/TD]
[/TR]
[TR]
[TD]Folder 3[/TD]
[TD]File name 2[/TD]
[/TR]
[TR]
[TD]Folder 3[/TD]
[TD]File name 3[/TD]
[/TR]
[TR]
[TD]Folder 3[/TD]
[TD]File name 4[/TD]
[/TR]
[TR]
[TD]Folder 4[/TD]
[TD]File name 1[/TD]
[/TR]
[TR]
[TD]Folder 4[/TD]
[TD]File name 2[/TD]
[/TR]
</tbody>[/TABLE]
Whenever I run my current macro, it's timing out and I'm not sure what's going on. Here's my current code:
Code:
Sub Iterate_Folders()Dim Row As Integer
Dim column As Integer
Dim Path As String
Dim Folder As String
Dim File As String
Row = 1
Path = "R:\Reinsurers\2. Bios\" ' Path should always contain a '\' at end
Folder = Dir(Path, vbDirectory) ' Retrieving the first entry.
File = Dir(Path & Folder & "\")
MsgBox File
Do Until Folder = "" ' Start the folder loop.
If (GetAttr(Path & Folder) And vbDirectory) = vbDirectory Then
If Path & Folder & "\" <> "" Then
ActiveSheet.Cells(Row, 1).Value = Folder
Do Until File = ""
If (GetAttr(Path & Folder & "\" & File) And vbDirectory) = vbDirectory Then
If Path & Folder & "\" & File <> "" Then
ActiveSheet.Cells(Row, column).Value = File
End If
End If
Loop
End If
Row = Row + 1
End If
Folder = Dir() ' Getting next entry.
Loop
End Sub
If I take out the second dimension that grabs the file names, I can correctly get the macro to grab the folder name and print them in Column A. However, when I add in the second-dimension loop, it times out and my computer is seemingly about to explode. Any help is appreciated. Thank you!