Run time error '53': File not found

haribole

New Member
Joined
Apr 10, 2018
Messages
19
Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim objFSO, strTextFile, strData, arrLines, LineCount
Dim ReceivedTrans, Mis As Integer
Dim MyFolder, MyFile, MyFileName, MySheetName, ReportDate As String
Dim FileType As String
Dim i, j As Integer
Const ForReading = 1
i = 1
j = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
MyFolder = "D:\TotalCompare"
MyFile = Dir(MyFolder & "\*.txt")
Do While MyFile <> ""
    If Left(MyFile, 21) = "ReverseFile_FOOD4514_" Or Right(MyFile, 12) = ".xls_RES.txt" Then
           If Left(MyFile, 21) = "ReverseFile_FOOD4514_" Then
                   strTextFile = MyFile
                   strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll
                   arrLines = Split(strData, vbCrLf)
                   LineCount = UBound(arrLines)
                   Set objFSO = Nothing
              MySheetName = Replace(MyFile, "txt", "")
              ReportDate = Right(Replace(MySheetName, ".", ""), 8)
              MyFileName = Replace((Replace(MySheetName, "_" & ReportDate & ".", "")), "ReverseFile_FOOD4514_", "")
              ReceivedTrans = LineCount
              On Error Resume Next
                   strTextFile = MyFileName & ".txt"
                   Set objFSO = CreateObject("Scripting.FileSystemObject")
                   strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll
                   arrLines = Split(strData, vbCrLf)
                   LineCount = UBound(arrLines)
                   Set objFSO = Nothing
              Mis = LineCount
          End If
    End If
MyFile = Dir
 Loop
 
I'm not seeing that error on my PC, it opens the file and reads the content into strData.

Try using Set strData= objFSO.OpenTextFile(.......etc

also sort out DIMs
Dim objFSO, strTextFile, strData, arrLines, LineCount ??? Objects, strings and integers
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Thank You :) Please see:
Code:
Dim objFSO As Object
Dim strTextFile As String
Dim strData As Variant
Dim arrLines() As String
Dim LineCount As Integer
Dim MyFolder, MyFile, MyFileName As String
Dim i As Integer
Const ForReading = 1
i = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
MyFolder = "D:\TotalCompare"
MyFile = Dir(MyFolder & "\*.txt")
Do While MyFile <> ""
                   strTextFile = MyFolder & "\" & MyFile
                   strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll
                   arrLines = Split(strData, vbCrLf)
                   LineCount = UBound(arrLines)
                   Set objFSO = Nothing
            MyFileName = Replace(MyFile, ".txt", "")
            Worksheets(1).Cells(i + 1, 1) = i
            Worksheets(1).Cells(i + 1, 2) = MyFileName
            Worksheets(1).Cells(i + 1, 3) = LineCount
    MyFile = Dir
Loop
 
Upvote 0
Hi

There's no file system object when you loop to the second file as you have set it to nothing inside of the loop.
Set it to nothing outside of the loop.

Code:
   MyFile = Dir
Loop
Set objFSO = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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