Hi all,
I have written some code to import a non-delimited text file (dat). It works but it is so sloooow (take 15 minutes on a computer with quad core processors). The text file itself is about 2200 rows and 14 coulmns. Can anyone see how I could speed up this code?
Cheers
Rob
I have written some code to import a non-delimited text file (dat). It works but it is so sloooow (take 15 minutes on a computer with quad core processors). The text file itself is about 2200 rows and 14 coulmns. Can anyone see how I could speed up this code?
Code:
Sub TxtfileImport()
Dim i As Long
Dim InFile As String, Str As String
Dim FileNum As Integer, LineLen As Integer
Dim DDate As Date
Application.ScreenUpdating = False
Application.EnableEvents = False
DDate = InputBox("Date please", "Date")
FileNum = 1
InFile = "C:\temp\Test_" & Format(DDate, "YYYYMMDD") & ".dat"
Open InFile For Input As FileNum
With ThisWorkbook.Worksheets("Sheet1")
Do While Not EOF(FileNum)
Line Input #FileNum, Str
If Left(Str, 2) = "02" Then
Cells(i, 1) = Left(Str, 2)
Cells(i, 2) = Mid(Str, 3, 20)
Cells(i, 3) = Mid(Str, 23, 12)
Cells(i, 4) = Mid(Str, 35, 8)
Cells(i, 5) = Mid(Str, 43, 30)
Cells(i, 6) = Mid(Str, 73, 30)
Cells(i, 7) = Mid(Str, 103, 15)
Cells(i, 8) = Mid(Str, 118, 1)
Cells(i, 9) = Mid(Str, 119, 16)
Cells(i, 10) = Mid(Str, 135, 16)
Cells(i, 11) = Mid(Str, 151, 16)
Cells(i, 12) = Mid(Str, 167, 16)
Cells(i, 13) = Mid(Str, 183, 16)
Cells(i, 14) = Mid(Str, 199, 16)
End If
i = i + 1
Loop
End With
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Cheers
Rob