Using VBA/Excel to Search for Files in Windows Explorer

kyosu kzlusterdauk

New Member
Joined
Sep 23, 2015
Messages
3
Hi,

I have a list of saved file names in descending order (without file type extension)

Column A Column B

File Name Link
A1
A2
A3
A4
etc

I would like to run a macro that searches for files containing the text of the file name (from column A) in a specified directory in windows explorer and then pastes a link to the file in the column (column B) next to the reference file name in excel, going thru the file names one by one. Where no file is located there would be simply a blank in Column B.

I am using Windows 2007 and understand that the application.filesearch function may not be activated and would have to use a findfiles function.


Can anyone help me out with a macro that would achieve this?
 
Found a solution

1. Enable microsoft scripting runtime

2.
Code:
'Creating a FileSystemObjectPublic FSO As New FileSystemObject
Sub ListFiles()
'Declaring variables
Dim objFolder As Folder
Dim objFile As File
Dim strPath As String
Dim NextRow As Long
'Specify the path of the folder
strPath = "T:FolderName"
'Create the object of this folder
Set objFolder = FSO.GetFolder(strPath)
'Check if the folder is empty or not
If objFolder.Files.Count = 0 Then
  MsgBox "No files were found...", vbExclamation
  Exit Sub
End If
'Adding Column names for A, B, and C
Cells(1, "A").Value = "File Name"
Cells(1, "B").Value = "Size"
Cells(1, "C").Value = "Modified Date/Time"
'Find the next available row
NextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
'Loop through each file in the folder
For Each objFile In objFolder.Files
'List the name, size, date/time and hyperlink of the current file
Cells(NextRow, 1).Value = objFile.Name
Cells(NextRow, 2).Value = objFile.Size
Cells(NextRow, 3).Value = objFile.DateLastModified
Cells(NextRow, 4).Value = objFile.Path
'Find the next row
NextRow = NextRow + 1
Next objFile
End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top