Consulting

Results 1 to 4 of 4

Thread: Reading in a text file and only printing certain lines

  1. #1

    Reading in a text file and only printing certain lines

    AK_Beaver.zip

    So I'm trying to read in a text file opus.txt and print it out into the immediate window. The print out needs to look like opus_output_1. Is there a way to only print out certain lines?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Add the correct line numbers to the array
    Option Explicit
    
    
    Function ReadAllTextFile()
       Const ForReading = 1
       Dim fso, f
       Dim txt
       Dim i As Long
       Dim arr, a
       
       arr = Array(1, 3, 5, 7)
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set f = fso.OpenTextFile("c:\vbax\opus.txt", ForReading)
       txt = Split(f.ReadAll, vbCr)
       For Each a In arr
       Debug.Print txt(a)
       Next
    End Function
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    So I have this code, which reads in the text file and prints the first 103 lines (i have option base 1 on). How would I go about changing this so it only prints out the lines I want?


    Sub OpusReader()    
    Dim Delay As Single
    Dim FileName As String
    Dim LineText As String
    Dim Text As String
    Dim i As Long
         
    FileName = GetImportFileName()
    
    
    Open FileName For Input As #1
    While Not EOF(1)
        i = i + 1
        If i = 104 Then GoTo Exits
        Line Input #1, LineText
        Debug.Print LineText
        Delay = Timer + 0.0025
        While Timer < Delay: DoEvents: Wend
                 
        Wend
    
    
    Exits:
        Close #1
    
    
    End Sub

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I wouldn't. I would use the code I posted with the file path amended and line numbers added to the array. I'll leave you to check the correct lines.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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