Input Lines from a Text File

A

Access101

Can you START at a chosen line within a text file, or do you always have to
start at line one.

For that matter, can you start at the end of the file, and work your way
backward?
Open "C:\text.txt" for input as #1

Do While Not EOF(1)
Line Input #1, strVal
 
S

Stuart McCall

Douglas J. Steele said:
You have to read sequentially from the front of the file.

Well one way to work backwards through a text file would be to open the file
for binary input and get the whole contents into a string. This string could
be passed to the VBA Split function, to load an array. The array can be
iterated forwards or backwards.

Air code:

Dim f As Integer
Dim b As String
Dim a As Variant
Dim i As Long

f = FreeFile()
Open "MyFile.txt" For Binary Access Read As f
b = Space$(Lof(f)) 'Allocate buffer sized to file
Get #f, , b 'Grab file's contents
Close f

a = Split(b, vbCrLf) 'Get "lines" into array

'Iterate from top to bottom, using 'Step -1'
For i = Ubound(a) To Lbound(a) Step -1
If <condition met> Then
'Do something
End If
Next

Erase a 'Free up ram allocated to array

What happens next depends on what you're doing..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top