Looping through an open file

Hap

Well-known Member
Joined
Jul 20, 2005
Messages
647
I have opened a text file and am attempting to loop through the lines of data. However, using the EOF function I can't seem to reset this setting without closing and reopening the file. So what's happening is when I try to start a new loop it recognizes that it's already at the end of the file and skips over the next loop. I'm assuming I'm missing something obvious. Any help would be greatly appreciated.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
A text file is not usually opened for random access -- you can't back it up.

Why not read the data into memory and loop there?
 
Upvote 0
Would I effectively be reading to an array and then looping through the array? Mostly I have been trying to be conscious of execution speed.
 
Upvote 0
It will be much faster to loop through an array, if the whole file fits in memory.
 
Upvote 0
I haven't worked with arrays much. Are vb arrays dynamic? if so whats the best way to determine the extents of the loops?

Thank you
 
Upvote 0
VBA arrays can be dynamic. Depending on the size of the file, you could read the file into a string variable and then use the Split function to separate it into lines.
 
Upvote 0
VBA arrays can be dynamic. Depending on the size of the file, you could read the file into a string variable and then use the Split function to separate it into lines.
To follow up on shg's message, here is some code to get you started...

Code:
Dim FileNum As Long, TotalFile As String, Lines() As String
FileNum = FreeFile
Open PathAndFileName For Binary As #FileNum
  TotalFile = Space(LOF(FileNum))
  Get #FileNum, , TotalFile
Close #FileNum
Lines = Split(TotalFile, vbNewLine)
After the above code runs, the Lines array contains each Windows newline character sequence (Carriage Return followed by a Line Feed) delimited line within the file in a separate array element.
 
Upvote 0
This is what I have so far although it is in a infinite loop so I'm missing something. I was going to determine the number of columns in my aArray and redim similar to what I was doing for each line. Whaddya think?

Do Until EOF(1)
Line Input #1, aLine
If InStr(1, aLine, "Type", 1) Then

For i = 1 To 256
If GetElement(aLine, ",", i) = "" Then
j = j + 1
End If
Next i
Exit Do
End If
Loop

ReDim aArray(1, j)
k = 1
Do Until EOF(1)
Line Input #1, aLine
For i = 1 To j
Do Until i = j
aArray(k, i) = GetElement(aLine, ",", i)
Loop
ReDim Preserve aArray(UBound(aArray) + 1, j)
k = k + 1
Next i
Loop
 
Upvote 0
The GetElement function I believe is something from CPearson's site. It returns the value at the i location in the aline.
 
Upvote 0
Could you explain what you're trying to do, without reference to your code?
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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