PDA

View Full Version : VBA - Textfile Skip line Help in Looping



malleshg24
07-16-2019, 11:47 AM
Hi Team


I am extracting data from text file line by line,
and I want to skip data seperator line
Below line whenever I come across. plz assist.Thanks.
---------------------------


Do Until ts.AtEndOfStream
line = ts.ReadLine
If InStr(line, "----*") > 0 Then
Debug.Print line
ts.SkipLine
Else
ActiveCell.Value = ts.ReadLine
ActiveCell.Offset(1, 0).Select
End If
Loop


Regards,
MG

Artik
07-16-2019, 02:30 PM
If you use
InStr(line, "----*") "---- *" is searched for exactly. You should use Like.


If you use:

ts.SkipLine
ts.ReadLine
the next line of text is skipped or read.

I think it would be more correct to use such code:

Sub ReadTextFile_Lineby_Line()
Dim fso As Scripting.FileSystemObject
Dim ts As Scripting.TextStream
Dim myFilePath As String
Dim line As String

Set fso = New Scripting.FileSystemObject

myFilePath = "E:\New folder\TextData.txt"

Set ts = fso.OpenTextFile(myFilePath, ForReading)

Sheet1.Range("A1").Select

Do Until ts.AtEndOfStream
line = ts.ReadLine
If Not line Like "----*" Then
ActiveCell.Value = line
ActiveCell.Offset(1, 0).Select
End If
Loop


ts.Close
Set fso = Nothing

End Sub

Artik