-
I need to take a text file, read it line by line, and put certain lines into an array...
I need some help on the reading line by line part...
Can anyone steer me in the right direction? I don't need to read it all into an array, just 1 line at a time, and if the line meets certain criteria, put THAT LINE in an array...(I can handle the "if it meets certain criteria" part...)
Thanks!!
-
Something along the lines of.
Code:
Open "myTxt.txt" For Input As #1
Do While Not Eof(1)
Line Input #1, myVariabe
Loop
Close #1
Hope that helps.
-
Or you could put it directly into a Perfect Fitting array like so:
Dim ln$(10)
lines = 0: Erase ln$: maxlines = 10:
Do While Not EOF(1)
Call createnextline((maxlines))
Line Input #1, ln$(lines)
ln$(lines) = RTrim$(ln$(lines))
Loop
Public Sub createnextline(maxlines)
lines = lines + 1
If lines + 20 >= maxlines Then
maxlines = lines + 40
ReDim Preserve ln$(maxlines)
End If
End Sub
Good luck...
-
"or"
or.....'the results are what you want.
'list box is only for a visula of what is happening
'you can remove it when satisfied you get what
'you are after
Option Explicit
Option Compare Text
Dim filesys, txtStream As Object
Private Sub Form_Load()
Dim myArr() As Variant
Dim i As Integer
Set filesys = CreateObject("Scripting.FileSystemObject")
Set txtStream = filesys.openTextFile("C:\my documents\x.txt")
Do Until txtStream.atendofstream
i = i + 1
ReDim Preserve myArr(1 To i) As Variant
myArr(i) = txtStream.readline
myArr(i) = Trim(myArr(i))
If myArr(i) = "Whatever line you want to look for" Then
List1.AddItem myArr(i)
End If
Loop
End Sub