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 01, 2020Silver Contributor
Can you upload a sample file so that we can see what you are trying to achieve?
Does the column have a full path of the file or only file names? Does any column or cell contains the folder path to look for those files?
And if the file is found in the target folder, what do you want to do then? Insert a VLookup Formula referring to those file names in the formula?
A sample file will help to find the answer to the above queries.
Ozz_Kozz
Oct 01, 2020Copper Contributor
Subodh_Tiwari_sktneerHi Thank you for your reply. I'll post a picture and hopefully it can come across clearer. In the example below there are 7 clients with file names in column D and 38 files in the folder. I only need to target the 7 files. The only criteria I have is the file name. What I would do is copy and paste the file name from the spreadsheet into the folder search box. That would work if I were only looking for 7 files by name in a folder with 38 files, but in real life I have many more file names in my spreadsheet and over 1,000 files in the folder. I compared it to VLOOKUP because if the folder were a spreadsheet, I could easily create a column and run the formula to match the files based on the name. Essentially I would want to be able to either highlight the names or somehow extract the files from the large folder. Please let me know if this make sense and if I'm on the right track. Thank you!
- Subodh_Tiwari_sktneerOct 02, 2020Silver Contributor
Let me know if I understood your requirement.
All you want to do is, check whether the files listed in column D are found in the Document folder.
e.g. Check if the file tps report.xls listed in cell D2 is found in the Document folder and if it exists in that folder, highlight the cell D2 which would be like a visual indicator that this file was found in the folder and do the same for all the files in column D.
Is that what you are trying to achieve?
- Ozz_KozzOct 02, 2020Copper Contributor
Subodh_Tiwari_sktneer Hi, yes but if exist in the Document Folder it should be highlighted in the Document Folder. I'm trying to avoid typing in the file name into the search box of the Document folder and save some time. I would want to find the file and extract it from the large folder. Hope that explains a bit more. Thanks.
- Subodh_Tiwari_sktneerOct 03, 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 Sub