VBA Code To Search Files in Folder

Jar888

Board Regular
Joined
Jan 15, 2022
Messages
61
Office Version
  1. 2016
Platform
  1. Windows
Got a stump of a question. Is there a way to search for files in the folder as shown in the attachment based off column A using VBA and either highlight the cell to indicate that that file exists or copy to a new directory? To give you an idea, I am assuming 98% of these files don't exist but I have over 9000 to check through.

As you can see, file names have an underscore prior to the order number displayed.

1648524921541.png
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You could use this to get all the file names from your archive folder into excel an then do a match or vlookup with the info

VBA Code:
Sub Get_FileNames()
Dim j As Long
    Dim xDirect$, xFname$, InitialFold
    Dim fldpath
 

    InitialFoldr$ = "C:\"    '<<< Startup folder to begin searching from

    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder to list Files from"
        .InitialFileName = InitialFoldr$
        
        .Show
        fldpath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1) & "\"
 
        Workbooks.Add
Cells(1, 1).Value = fldpath
Cells(2, 1).Value = "FILE NAME"

Set fso = Nothing
Range("a1").Font.Size = 9
ActiveWindow.DisplayGridlines = True
Range("a3:e" & Range("a2").End(xlDown).Row).Font.Size = 9
Range("a2:e2").Interior.Color = vbYellow
Columns("c:h").AutoFit
Application.ScreenUpdating = True


        If .SelectedItems.Count <> 0 Then
            xDirect$ = .SelectedItems(1) & "\"
            xFname$ = Dir(xDirect$, 7)
            Do While xFname$ <> ""
            j = Range("A1").End(xlDown).Row + 1
Cells(j, 1).Value = xFname$
        
                xFname$ = Dir
          
            Loop
         End If
        
    End With

    ActiveWorkbook.Save
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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