VBA text file read fails when large

krausr79

Board Regular
Joined
Feb 12, 2012
Messages
209
So I'm reading a text file with extension .DAT from the file into excel. I assembled a method to do this from a few different posts. Here is the method which works:
Sub LoadFile()
Dim objFSO As Object
Dim objTF As Object
Dim strIn As String
Dim X
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile("C:\DL\DATATEST.DAT", 1)
strIn = objTF.readall
X = Split(strIn, vbNewLine)
Range("A1:A" & UBound(X) + 1) = Application.WorksheetFunction.Transpose(X)
objTF.Close
End Sub

But it stops working on a larger (11 MB) file. I get a Error 13 Type mismatch. It doesn't seem to be related to the data, because if I delete either the first or second half of all the data it works on either smaller file.

A line-by-line reading method is no good because that takes about 1 second per 500 lines and I've got upwards of 120k.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
error 13 means its trying to put a string into a numeric variable.

Did you try importing via FILE, OPEN? (instead of using code)
 
Upvote 0
If you want to read the text file into Excel, why not read it as QueryTable to import it?
 
Upvote 0
I am able to import the data manually in the ways you two describe. In vba, I'd rather not deal with learning the particulars of querytables and I also didn't want to leave a data connection in the workbook. I found a thread with a solution I like by using your ideas as search terms:

vba: Importing text file into excel sheet - Stack Overflow
Sub Sample()
Dim wbI As Workbook, wbO As Workbook
Dim wsI As Worksheet
Set wbI = ThisWorkbook
Set wsI = wbI.Sheets("Sheet1") '<~~ Sheet where you want to import
Set wbO = Workbooks.Open("C:\OMEGA\P-19_071015.DAT")
wbO.Sheets(1).Cells.Copy wsI.Cells
wbO.Close SaveChanges:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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