I have a list of strings in column, and each string is a filename.
I'm trying to output the file path on column B.
I have tweaked a macro I found that will cycle through a given main folder and, if the string in column A matches a filename, it will output the path in column B.
However, I can't get it to cycle through the whole list of strings in column A, but only the first string. Can you help me modify it so that it loops throughout the whole column (number of rows will vary, no blank cells).
P.S I posted a couple days ago looking for a similar solution trying to tweak another macro, but that went nowhere. This one is more promising as it works for one cell, just not for all.
I'm trying to output the file path on column B.
I have tweaked a macro I found that will cycle through a given main folder and, if the string in column A matches a filename, it will output the path in column B.
However, I can't get it to cycle through the whole list of strings in column A, but only the first string. Can you help me modify it so that it loops throughout the whole column (number of rows will vary, no blank cells).
Code:
Function Recurse(sPath As String) As String
Dim FSO As New FileSystemObject
Dim myFolder As Folder
Dim mySubFolder As Folder
Dim myFile As File
Set myFolder = FSO.GetFolder(sPath)
For Each mySubFolder In myFolder.SubFolders
For Each myFile In mySubFolder.Files
If myFile.Name = Range("A2").Value Then 'This is where it matches the filename with the string, starting from A2
Range("B2").Value = myFile.Path 'if it finds a match it outputs the full path + filename in column B, starting from B2
Exit For
End If
Next
Recurse = Recurse(mySubFolder.Path)
Next
End Function
Code:
Sub TestR()
Dim x As Integer
ActiveSheet.Select
NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
For x = 1 To NumRows 'I played around with this trying to get it to loop through the column A rows but it doesn't work.
Call Recurse("H:\test")
Next
End Sub
P.S I posted a couple days ago looking for a similar solution trying to tweak another macro, but that went nowhere. This one is more promising as it works for one cell, just not for all.