Ok, so I consider myself an Excel VBA expert (even though I\'ve not done much with it for a while) but I\'m stumped on this one - that obviously means it is something extrem
I think you want to list all files in all folders and all sub-folders. Check out this link.
http://www.learnexcelmacro.com/wp/2011/11/how-to-get-list-of-all-files-in-a-folder-and-sub-folders/
Download the file; that's the way to go. Once all paths and all file names are listed in your Excel worksheet, you can do all kinds of comparisons, manipulations, and the like.
Sub GetFilesInFolder(SourceFolderName As String)
'--- For Example:Folder Name= "D:\Folder Name\"
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.folder, SubFolder As Scripting.folder
Dim FileItem As Scripting.File
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
'--- This is for displaying, whereever you want can be configured
r = 14
For Each FileItem In SourceFolder.Files
Cells(r, 2).Formula = r - 13
Cells(r, 3).Formula = FileItem.Name
Cells(r, 4).Formula = FileItem.Path
Cells(r, 5).Formula = FileItem.Size
Cells(r, 6).Formula = FileItem.Type
Cells(r, 7).Formula = FileItem.DateLastModified
Cells(r, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open" & """)"
r = r + 1 ' next row number
Next FileItem
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub
Sub GetFilesInFolder(SourceFolderName As String, Subfolders As Boolean)
'--- For Example:Folder Name= "D:\Folder Name\" and Flag as Yes or No
Dim FSO As Scripting.FileSystemObject
Dim SourceFolder As Scripting.folder, SubFolder As Scripting.folder
Dim FileItem As Scripting.File
'Dim r As Long
Set FSO = New Scripting.FileSystemObject
Set SourceFolder = FSO.GetFolder(SourceFolderName)
'--- This is for displaying, whereever you want can be configured
r = 14
For Each FileItem In SourceFolder.Files
Cells(r, 2).Formula = r - 13
Cells(r, 3).Formula = FileItem.Name
Cells(r, 4).Formula = FileItem.Path
Cells(r, 5).Formula = FileItem.Size
Cells(r, 6).Formula = FileItem.Type
Cells(r, 7).Formula = FileItem.DateLastModified
Cells(r, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open" & """)"
r = r + 1 ' next row number
Next FileItem
'--- This is the Function to go each and Every Folder and get the Files. This is a Nested-Function Calling.
If Subfolders = True Then
For Each SubFolder In SourceFolder.Subfolders
ListFilesInFolder SubFolder.Path, True
Next SubFolder
End If
Set FileItem = Nothing
Set SourceFolder = Nothing
Set FSO = Nothing
End Sub
Ok - turns out the issue was actually with the folder and not the code. As pointed out by @Peh, I should have tested the code with another folder, as well as testing variations of the code itself. When using another folder, the For Each
code worked fine. As stated in the OP, I just used For i = 1 to xFolder.Files.Count
rather than For Each
and got the result I needed, but I prefer the object-led approach of For Each
rather than using an integer/long variable to go through the item count, and wanted to know why that method wasn't working.
To get the For Each
code working, I copied the desired files to a different folder and it worked perfectly well. As the original folder was a network location, there was possibly some folder permissions or security setting preventing me from using the code I wanted to.
This is a working minimal example:
BGD
and write an existing path into C4
e.g. C:\Windows
This lists the path and all filenames in the debug window.
Public Sub GetData()
Dim bgd As Worksheet
Dim myFSO As FileSystemObject
Dim xFolder As Scripting.Folder
Dim xFile As Scripting.File
Set bgd = ThisWorkbook.Sheets("BGD")
Set myFSO = New FileSystemObject
Set xFolder = myFSO.GetFolder(bgd.Range("C4").Value)
Debug.Print xFolder.Path
For Each xFile In xFolder.Files
Debug.Print xFile.Name
Next xFile
End Sub
If you need the variables locally then declare them locally instead of global. Anything else is very bad practice and leads into errors.