Forum Discussion
Matching file names in Excel column to file names in folder
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!
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
- AnthonyDavisSFAug 18, 2023Copper ContributorI'm very new to this, so thank you for what you've provided so far! I'm wondering if someone could help me tweak some things, as I can't seem to get the code to work...
I've gotten it to create the subfolder in the source folder, but I would like to have the program search for partial file names instead of whole names (ex: search for 20448 results in the file labeled: 20448 xxxx-xxxxxxxxxx, xxx-xxx, xxxxx-xx)
I'm wondering what I would need to add to this code to have it return the correct files with only the digits at the beginning of the file name.
Thanks in advance!