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
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Once again, you might give us a clue as to where the error occurs? Just posting a block of code with no explanation not only doesn't help us to help you, it doesn't really encourage us to try...;)
 
Upvote 0
Once again, you might give us a clue as to where the error occurs? Just posting a block of code with no explanation not only doesn't help us to help you, it doesn't really encourage us to try...;)

I kinda liked the challenge :)
 
Upvote 0
WBD - Does that account for?

strTextFile = MyFileName & ".txt"
 
Upvote 0
So sorry !! Please !!

I stuck here with Run time error 53 msg:

strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll
 
Upvote 0
So sorry !! Please !!

I stuck here with Run time error 53 msg:

strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll

Dir() returns only the file name; not the full path. You need to add the path on to the beginning or add a line near the beginning:

Code:
ChDir MyFolder

WBD
 
Upvote 0
Accordingly I have added path (and minimized the code) but still not working !!:LOL:

Please help !!

Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim objFSO, strTextFile, strData, arrLines, LineCount
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   'Added the path
                   strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll      'Here is the error msg [I][COLOR=#ff0000]Run time error '91': Object variable or Block variable not set[/COLOR][/I]
                   arrLines = Split(strData, vbCrLf)
                   LineCount = UBound(arrLines)
                   Set objFSO = Nothing
            MyFileName = Replace(MyFile, ".txt", "")
            Worksheets(1).Cells(i + 1, 1) = i         'Fist row having heading
            Worksheets(1).Cells(i + 1, 2) = MyFileName
            Worksheets(1).Cells(i + 1, 3) = LineCount
    MyFile = Dir
Loop
 
Upvote 0

Forum statistics

Threads
1,214,409
Messages
6,119,339
Members
448,888
Latest member
Arle8907

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