-
Reading in a Text File
I can read a text file with the following code, but the issue is that "RowEnd" must be known before hand. Is there a way to determine the textline where "This is the stop text" is and then feed that to RowEnd? I basically want it to stop reading in a certain point in the text file that is where a given textline occurs, not at a fixed value.
Code:
Do Until textline = "Start Line"
Line Input #1, textline
Loop
For j = RowStart To RowEnd
Line Input #1, textline
newline = Split(textline, ",")
Cells(j, 1) = newline(0)
Cells(j, 2) = newline(1)
Cells(j, 3) = newline(2)
Cells(j, 4) = newline(3)
Next j
-
Please show the complete code, perhaps wit worksheet
-
Code:
Sub M_snb()
open "G\OF\example.txt" for input as #1
msgbox split(Input(LOF(1),#1),"This is the stop Text")(0)
close
End Sub
-
You can try something like this - it's not tested, and can be made more elegant, but this seemed the simplest to start with
Code:
Option Explicit
Sub test()
Dim iFile As Long, j As Long
Dim bStart As Boolean
Dim textline As String
Dim newline As Variant
iFile = FreeFile
bStart = False
Do While Not EOF(iFile)
j = j + 1
Line Input #1, textline
If textline = "Start line" Then
bStart = True
GoTo Nextline
ElseIf textline = "This is the stop text" Then
Exit Do
ElseIf bStart Then
newline = Split(textline, ",")
Cells(j, 1) = newline(0)
Cells(j, 2) = newline(1)
Cells(j, 3) = newline(2)
Cells(j, 4) = newline(3)
End If
Nextline:
Loop
Close #iFile
End Sub