View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone[_2_] Jim Cone[_2_] is offline
external usenet poster
 
Posts: 1,549
Default Read .TXT file line by line?

My turn <g ...
Making the assumption you are using xl2007/2010, suggest you change
lngMaxRows to ~250000
ARR_Size to ~ 50,000

The above changes would give you ~12 columns with 250,000 rows in each column.
(something to tell Grandma about if it doesn't blow up)
'---
Public Function ReadTextFile(ByRef strFullPath As String, _
ByRef WS As Excel.Worksheet) As String
'JBC - Portland, Oregon USA - December, 2008
Dim objFso As Object
Dim oTextFile As Object
Dim colCounter As Long
Dim lngMaxRows As Long
Dim lngCount As Long
Dim lngMarker As Long
Dim N As Long
Dim strTemp As String
Dim vArr() As String
Const ARR_Size As Long = 20000

On Error GoTo ErrHandler
N = 1
lngCount = 1
colCounter = 1
lngMaxRows = CLng(WS.Rows.Count \ ARR_Size)
lngMaxRows = lngMaxRows * ARR_Size 'maximum rows used on sheet
ReDim vArr(1 To ARR_Size, 1 To 1)
Set objFso = CreateObject("Scripting.FileSystemObject")

Application.ScreenUpdating = False
'Reads every line and adds it the the array.
If objFso.FileExists(strFullPath) Then
Set oTextFile = objFso.OpenTextFile(strFullPath)
Do While Not oTextFile.AtEndOfStream
strTemp = oTextFile.Readline
If Len(strTemp) Then
vArr(N, 1) = strTemp
N = N + 1
If N lngMaxRows Then
DoEvents
'Add array to sheet, switch to next column, reset variables.
WS.Range(WS.Cells(LBound(vArr(), 1), colCounter), _
WS.Cells(UBound(vArr(), 1), colCounter)).Value = vArr()
colCounter = colCounter + 1
lngMarker = 1
lngCount = 1
N = 1
ReDim vArr(N To ARR_Size, 1 To 1)
ElseIf N (ARR_Size * lngCount) Then
'Add array to sheet
WS.Range(WS.Cells(LBound(vArr(), 1), colCounter), _
WS.Cells(UBound(vArr(), 1), colCounter)).Value = vArr()
ReDim vArr(N To (N + ARR_Size), 1 To 1)
'Keep track of how many times array added to the same column.
lngCount = lngCount + 1
'Flag to identify if partially filled array exists when loop completes.
lngMarker = N
ElseIf N Mod 5000 = 0 Then
Application.StatusBar = "Row " & N & " - Column " & colCounter
End If
End If
Loop
'If a partially filled array leftover, add it to sheet
If N lngMarker Then WS.Range(WS.Cells(LBound(vArr(), 1), colCounter), _
WS.Cells(UBound(vArr(), 1), colCounter)).Value = vArr()
End If

ExitRoutine:
On Error Resume Next
oTextFile.Close
Set oTextFile = Nothing
Set objFso = Nothing
Application.StatusBar = False
Application.ScreenUpdating = True
Exit Function

ErrHandler:
Resume ExitRoutine
End Function
'---

Jim Cone
Portland, Oregon USA .
http://www.mediafire.com/PrimitiveSoftware .
(Formats & Styles xl add-in: lists/removes unused styles & number formats) - free






"Charlotte E."
wrote in message
...
How to read a .TXT-file line by line?

I would like to read the first line of the .TXT-file,
Perform some action on the read data (the .TXT-line)

Read the next line - perform some action
Read the next line - perfom the action again
etc...

Until all lines of the .TXT-file is read, and the action is performed on all lines (imported data)

My problem is that I have a HUGE .TXT-file of almost 3.000.000 lines!!!
I'm aware that going through all these lines, one-by-one, will probably take a week or so, so I
have to make my first test-program read a smaller file, until I'm sure it is working.

But, how to read the file, line by line, in the first place, since I can't import such a huge file
in my trusty old XL2003?

Oh, I case you wonder what the 'action' is, I want to remove those lines in the .TXT-file that
doens't fullfill certain criteria, thus (hopefully :-) ending up with a .TXT-file of only approx.
105.000 lines, which then can be converted into a webtable.


Thanks in advance...

CE