Forum Discussion
Ozz_Kozz
Sep 30, 2020Copper Contributor
Matching file names in Excel column to file names in folder
Hi, I have a column in excel that contains the file names of files that are in a folder (ex: IMG_001, contacs.pdf, essay.docx). I have about 30 rows with miscellaneous file names in the column, and ...
Subodh_Tiwari_sktneer
Oct 02, 2020Silver Contributor
Okay, please try the following code and see if this is something you can work with.
The following code will check the files listed in column 4 of the Table on Sheet called "B" in the Document folder and if it finds the file in that folder, it will highlight the cell with filename in the Table and copy the file from Document folder to a Sub-Folder called "Found Files".
Sub SearchFiles()
Dim ws As Worksheet
Dim tbl As ListObject
Dim cel As Range
Dim rootFolder As String
Dim strNameNewSubFolder As String
Dim fso As FileSystemObject
Dim newFolder As Folder
Dim fil As File
Dim strFilepath As String
Dim newFilePath As String
Set fso = New FileSystemObject
Set ws = Worksheets("B")
Set tbl = ws.ListObjects(1)
'Path of the Source folder with files
rootFolder = "C:\Users\sktneer\Documents"
If Not fso.FolderExists(rootFolder) Then
MsgBox rootFolder & " doesn't exist.", vbExclamation, "Source Folder Not Found!"
Exit Sub
End If
'files that are found in the Source Folder would be copied to this New Sub-Folder
'Change the name of the Sub-Folder as per your requirement
strNameNewSubFolder = "Found Files"
If Right(rootFolder, 1) <> "/" Then rootFolder = rootFolder & "/"
If Not fso.FolderExists(rootFolder & strNameNewSubFolder) Then
fso.CreateFolder rootFolder & strNameNewSubFolder
End If
Set newFolder = fso.GetFolder(rootFolder & strNameNewSubFolder)
tbl.DataBodyRange.Columns(4).Interior.ColorIndex = xlNone
For Each cel In tbl.DataBodyRange.Columns(4).Cells
strFilepath = rootFolder & cel.Value
newFilePath = newFolder.Path & "/" & cel.Value
If fso.FileExists(strFilepath) Then
cel.Interior.Color = vbYellow
Set fil = fso.GetFile(strFilepath)
'The following line will copy the file found to the newly created Sub-Folder
fil.Copy newFilePath
End If
Next cel
Set fso = Nothing
End Subelmaralma
Aug 02, 2021Copper Contributor
Hello! - I would be happy to check with you the possibility of adjusting the code. Because in the current configuration, it does not really work for me.
I need the code to scan all the cells in column A, and if there is a match between the information in the cell and the same file name in the original folder where the excel file is saved, the code will create one new folder, somewhere changeable, and copy all files with a name similar to the data in each cell In column A for the location in the new folder.
I need the code to scan all the cells in column A, and if there is a match between the information in the cell and the same file name in the original folder where the excel file is saved, the code will create one new folder, somewhere changeable, and copy all files with a name similar to the data in each cell In column A for the location in the new folder.