G
Guest
Guest
I am writing a macro to get a list of subfolders and files in a folder and link the cells to the corresponding files. Here is the macro
Sub ShowAllList(folderspec)
Dim fs, f, f1, f2, f3, fc, fd, fe
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
Set fd = f.subfolders
i = 1
For Each f1 In fc
Cells(i, 1) = f1.Name
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:=f1
i = i + 1
Next
For Each f2 In fd
Cells(i, 1) = f2.Name
i = i + 1
Set fe = f2.Files
For Each f3 In fe
Cells(i, 1) = f2.Name
Cells(i, 2) = f3.Name
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 2), Address:=f3
i = i + 1
Next
Next
End Sub
Sub listallfiles()
ShowAllList ("h:kcy")
End Sub
It works if in there is only one level of subfolders (which is usually not the case). I want to rewrite it so that it can handle arbitray numbers of levels of subfolders. Can anybody help? Thank you.
Sub ShowAllList(folderspec)
Dim fs, f, f1, f2, f3, fc, fd, fe
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
Set fd = f.subfolders
i = 1
For Each f1 In fc
Cells(i, 1) = f1.Name
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:=f1
i = i + 1
Next
For Each f2 In fd
Cells(i, 1) = f2.Name
i = i + 1
Set fe = f2.Files
For Each f3 In fe
Cells(i, 1) = f2.Name
Cells(i, 2) = f3.Name
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 2), Address:=f3
i = i + 1
Next
Next
End Sub
Sub listallfiles()
ShowAllList ("h:kcy")
End Sub
It works if in there is only one level of subfolders (which is usually not the case). I want to rewrite it so that it can handle arbitray numbers of levels of subfolders. Can anybody help? Thank you.