Handle Error when FileCopy can't find file

concreteinterface

Board Regular
Joined
Jul 10, 2008
Messages
144
I thought I was doing this right, but apparently not. Getting a "File Not Found" error when the file is not present in the folder. I know that the file sometimes won't be in the first folder or both folders. I think my On Error line isn't correct. What am I missing here?

Code:
Sub Copy()

    SerialDate = Format(Date, "yymmdd") & Format(Time, "hhmmss")
    CopyFileLocation = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & SerialDate & "\"
    MkDir CopyFileLocation
    
    For i = 2 To 1000
        On Error Resume Next
        FileCopy "V:\fullfilepath1\" & Range("B" & i).Value & ".pdf", CopyFileLocation & Range("B" & i).Value & ".pdf"
        On Error GoTo ErrHandler
        FileCopy "V:\fullfilepath2\" & Range("B" & i).Value & ".pdf", CopyFileLocation & Range("B" & i).Value & ".pdf"
        GoTo SkipErr
        
ErrHandler:
            Range("A" & i).Value = "COULD NOT EXTRACT!"
            GoTo SkipErr2
SkipErr:
            Range("A" & i).Value = SerialDate
SkipErr2:
    
    Next


End Sub
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Got it. Found the correct way around.

Code:
Sub Copy()

'https://www.mrexcel.com/forum/excel-questions/1045260-handle-error-when-filecopy-cant-find-file.html#post5017621


Dim FilePath As String
Dim TestStr As String
    
    SerialDate = Format(Date, "yymmdd") & Format(Time, "hhmmss")
    CopyFileLocation = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & SerialDate & "\"
    MkDir CopyFileLocation
    
    For i = 2 To 10000
        If Range("B" & i).Value = "" Then GoTo Skip1
        If Range("A" & i).Value <> "" Then GoTo Skip1
        
        NewFilePath = CopyFileLocation & Range("B" & i).Value & ".pdf"
        
        FilePath = "V:\FILEPATH1\" & Range("B3").Value & ".pdf"
        TestStr = ""
        On Error Resume Next
        TestStr = Dir(TestStr)
        On Error GoTo 0
        If TestStr <> "" Then
            FileCopy TestStr, NewFilePath
            Range("A" & i).Value = SerialDate
            GoTo Skip1
        End If


        FilePath = "V:\FILEPATH2\" & Range("B" & i).Value & ".pdf"
        TestStr = ""
        On Error Resume Next
        TestStr = Dir(TestStr)
        On Error GoTo 0
        If TestStr <> "" Then
            FileCopy TestStr, NewFilePath
            Range("A" & i).Value = SerialDate
        Else
            Range("A" & i).Value = "COULD NOT EXTRACT!"
        End If
            
Skip1:
    
    Next


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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