VBA - Search files in folder, copy found files, return what files were not found

Norbert94

New Member
Joined
Aug 25, 2023
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
Hello,

What I am trying to achieve here is the following:

I have a column with filenames that includes the extension (in this case, .pdf) - these are plain values, not results of formulas
I'm trying to create a macro that loops through the column (that starts at B2), use the values on each row to search for the file in a predefined source folder - if found, it should copy the file to a predefined destination folder and, if not found, I would like it to write in the cell to the right "Not found", i.e if the file at B13 is not found, show the "Not found" message in cell C13.

This is the code I have:

Sub TestingN()

Dim sPath As String
Dim dPath As String
Dim fName As String
Dim ir As Long
ir = 2
Dim fso As FileSystemObject

Sheets("CE_Files").Activate

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the source folder"
.InitialFileName = sPath
If Not .Show Then Exit Sub
sPath = .SelectedItems(1)
End With

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the destination folder"
.InitialFileName = dPath
If Not .Show Then Exit Sub
dPath = .SelectedItems(1)
If Right(dPath, 1) <> "\" Then dPath = dPath & "\"
End With

fName = Cells(ir, 2)
Do While fName <> ""
If Dir(sPath & fName) <> "" Then
FileCopy sPath & fName, dPath & fName
Else: Cells(ir, 3) = "File Not Found"
End If
Loop


End Sub


It returns "File not found" for the first file, even though the file exists. And it the does not loop through the rest nor copy any of the files. Any suggestions?

Thanks!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I missed some steps there:

Sub TestingN()

Dim sPath As String
Dim dPath As String
Dim fName As String
Dim ir As Long
ir = 2
Dim fso As FileSystemObject

Sheets("CE_Files").Activate

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the source folder"
.InitialFileName = sPath
If Not .Show Then Exit Sub
sPath = .SelectedItems(1)
End With

With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Choose the destination folder"
.InitialFileName = dPath
If Not .Show Then Exit Sub
dPath = .SelectedItems(1)
If Right(dPath, 1) <> "\" Then dPath = dPath & "\"
End With

fName = Cells(ir, 2)
Do While fName <> ""
If Dir(sPath & fName) <> "" Then
FileCopy sPath & fName, dPath & fName
Else: If Not Dir(sPath & fName) <> "" Then Cells(ir, 3) = "File Not Found"
End If

ir = ir + 1
fName = Cells(ir, 2)

Loop


End Sub

Now it loops through all the values correctly, but it returns the "File not found" value to each of them, does not copy anything
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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