PDA

View Full Version : [SOLVED:] Reading in a text file and only printing certain lines



AK_Beaver
05-05-2017, 01:30 PM
19098

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?

mdmackillop
05-05-2017, 02:33 PM
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

AK_Beaver
05-05-2017, 02:44 PM
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

mdmackillop
05-05-2017, 02:50 PM
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.