Read a large txt file using VBA

Photomofo

Active Member
Joined
Aug 20, 2012
Messages
259
Here's a snippet of code that reads in a big txt file. It used to work but the size of the INPUT text file grew such that I'm now getting an out of memory error. On a whim I changed objTS.Read(objFile.Size) to objTS.Read(objFile.Size / 20) and the file is getting read again but apparently only 1/20th of it.

I'm looking for a widget that would break the file into smaller chunks but I can't seem to find one that works.

Any advice on a different way to approach this problem?

Code:
File_x = Application.GetOpenFilename()
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.GetFile(File_x)
Set objTS = objFile.OpenAsTextStream(ForReading, TristateFalse)
EBA_File = objTS.Read(objFile.Size / 20)


'Chop up EBA_File into Smaller Strings
'***************************************************************
k = Application.RoundUp(Len(EBA_File) / 50000000, 0)


For i = 1 To k


String_Index(i) = Mid(EBA_File, (i - 1) * 50000000 + 1, 51000000)


Next i


EBA_File = vbNullString
 
Yep... With this new data intake structure I should be able to adjust which lines of data I bring into the string array and eliminate the memory errors. Seems like as the file gets bigger I keep running into different limitations of the technique I'm using and have to rework the program. Fortunately this isn't a file I need to regularly process.

Thanks to all for your suggestions. I don't come here with my hat in hand unless I've been stuck for several hours. You guys are always great.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

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