Consulting

Results 1 to 2 of 2

Thread: VBA - Textfile Skip line Help in Looping

  1. #1
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location

    VBA - Textfile Skip line Help in Looping

    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
    Attached Files Attached Files

  2. #2
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •