Ting Ping
Ting Ping

Reputation: 1145

How to ignore error and continue with the Loop?

I get runtime errror 53: file not found for the 27th-28th item. Any idea what is wrong?

The error lies with:

"FileCopy Source:=SourcePath, Destination:=DestinationPath"

Option Base 1
Sub LoopThroughFolder()

    Const FileSpec As String = "*.xls"
    Dim y As Integer
    Dim MyFolder As String
    Dim MyFile As String
    Dim iDot As Integer
    Dim FileRoot As String
    Dim FileExt As String

    Dim SourcePath As String
    Dim DestinationPath As String

    Dim ArrayData() As Variant
    Dim Series() As Integer


    'Capture the filename information
    For y = 2009 To 2030
        ReDim Preserve ArrayData(12, y)
        ReDim Preserve Series(12, y)
        MyFolder = ActiveWorkbook.Path & "\" & y & "\"

        i = 1
        MyFile = Dir(MyFolder & FileSpec)
        Do While Len(MyFile) > 0
            iDot = InStrRev(MyFile, ".")

            If iDot = 0 Then
                FileRoot = MyFile
                FileExt = ""
            Else
                FileRoot = Left(MyFile, iDot - 1)
                FileExt = Mid(MyFile, iDot - 1)
            End If

            MyFile = Dir
            ArrayData(i, y) = FileRoot
            i = i + 1
        Loop
    Next y

    'Conversion from MMMYY to numerical sequence
    a = 1
    BasicPath = ActiveWorkbook.Path
    For y = 2009 To 2030
        For i = 1 To 12
            If Not IsEmpty(ArrayData(i, y)) Then
                Series(i, y) = a
                a = a + 1

                SourcePath = BasicPath & "\" & y & "\" & ArrayData(i, y) & ".xls"
                DestinationPath = BasicPath & "\output\" & "Bill_Summary_Report_" & Series(i, y) & ".xls"

                FileCopy Source:=SourcePath, Destination:=DestinationPath

            Else
                x = 0
            End If
        Next i
    Next y

End Sub

Upvotes: 0

Views: 2192

Answers (2)

Santosh
Santosh

Reputation: 12353

I have added a function fileExist which will true if path exist. Before this line "FileCopy Source:=SourcePath, Destination:=DestinationPath" is called it always better to checkif they exist and if yes then proceed with filecopy.

Option Base 1 Sub LoopThroughFolder()

    Const FileSpec As String = "*.xlsm"
    Dim y As Integer
    Dim MyFolder As String
    Dim MyFile As String
    Dim iDot As Integer
    Dim FileRoot As String
    Dim FileExt As String

    Dim SourcePath As String
    Dim DestinationPath As String

    Dim ArrayData() As Variant
    Dim Series() As Integer


    'Capture the filename information
    For y = 2009 To 2030
        ReDim Preserve ArrayData(12, y)
        ReDim Preserve Series(12, y)
        MyFolder = ActiveWorkbook.path & "\" & y & "\"

        i = 1
        MyFile = Dir(MyFolder & FileSpec)
        Do While Len(MyFile) > 0
            iDot = InStrRev(MyFile, ".")

            If iDot = 0 Then
                FileRoot = MyFile
                FileExt = ""
            Else
                FileRoot = Left(MyFile, iDot - 1)
                FileExt = Mid(MyFile, iDot - 1)
            End If

            MyFile = Dir
            ArrayData(i, y) = FileRoot
            i = i + 1
        Loop
    Next y

    'Conversion from MMMYY to numerical sequence
    a = 1
    BasicPath = ActiveWorkbook.path
    For y = 2009 To 2030
        For i = 1 To 12
            If Not IsEmpty(ArrayData(i, y)) Then
                Series(i, y) = a
                a = a + 1

                SourcePath = BasicPath & "\" & y & "\" & ArrayData(i, y) & ".xls"
                DestinationPath = BasicPath & "\output\" & "Bill_Summary_Report_" & Series(i, y) & ".xls"

                If fileExist(SourcePath) And fileExist(DestinationPath) Then
                    FileCopy Source:=SourcePath, Destination:=DestinationPath
                End If

            Else
                x = 0
            End If
        Next i
    Next y

End Sub

Function fileExist(path As String) As Boolean
    On Error Resume Next

    Dim file As String
    file = Dir(path)

    If file <> "" Then fileExist = True

    On Error GoTo 0
End Function

Upvotes: 0

shibormot
shibormot

Reputation: 1638

try

Sub LoopThroughFolder()
  on error resume next
  .....

Upvotes: 1

Related Questions