Astra

Excel VBA Code to Read Text File Line by Line

Estimated reading: 5 minutes

In general, lines are separated by breaking one after another, which can be determined by the VBA built-in function vbCRLF. However, this article will teach us several ways to read a text file line by line using VBA. 

Code 01: Read the Text File Line by Line using Line Input Statement

Sub Read_Text_File_Line_By_Line1()

    ' Open the text file for input
    Open "C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt" For Input As #1
    
    ' Loop through each line of the file
    Do While Not EOF(1)
        Line Input #1, textLine
        
        ' sohow the value of the textLine variable in message box
       MsgBox textLine
    Loop
    
    ' Close the file
    Close #1

End Sub

Note: Check the following references 

Excel VBA to Read Text File Line by Line

Result: Suppose we created a text file in the path (C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt) containing a portion of a poem by William Shakespeare. Then run the code by pressing F5. A message box will show every line of the poem. Click on OK for displaying the next line.

Excel VBA to Read Text File Line by Line1

Code 02: Read the Text File Line by Line using the ReadLine method

Sub Read_Text_File_Line_By_Line2()

    Dim fso As Object
    Dim textFile As Object
    Dim textLine As String
    
    ' Create a FileSystemObject instance
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Open the text file for reading
    Set textFile = fso.OpenTextFile("C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt", 1)
    
    ' Loop to rin the code for each line of the file
    Do Until textFile.AtEndOfStream
        textLine = textFile.ReadLine
        ' Show the each lines in message box
    MsgBox textLine
    Loop
    
    ' Close the file
    textFile.Close
    
    ' Release the FileSystemObject instance
    Set fso = Nothing

End Sub

Code 03: Read the Text File Line by Line by splitting with every vbCRLF Delimiter

Sub Read_Text_File_Line_By_Line3()

    Dim stream As Object
    Dim text As String
    Dim lines As Variant
    
    ' Create a new ADODB.Stream object
    Set stream = CreateObject("ADODB.Stream")
    
    ' Set the stream type to text and the charset to UTF-8
    stream.Type = 2 'adTypeText
    stream.Charset = "UTF-8"
    
    ' Open the text file and read its contents into the stream
    stream.Open
    stream.LoadFromFile "C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt"
    
    ' Split the stream contents into an array of lines
    lines = Split(stream.ReadText, vbCrLf)
    
    ' Loop through each line of the file
    For i = 0 To UBound(lines)
        text = lines(i)
        
        ' Display lines
        MsgBox text
    Next i
    
    ' Close the stream
    stream.Close
    
    ' Release the ADODB.Stream object
    Set stream = Nothing

End Sub

Example: Read the Text File and Save It to an Excel Sheet Line by Line

Sub Read_Text_File_Line_By_Line4()

    Dim PathOfFile As String
    Dim F_Number As Integer
    Dim Line_Txt As String
    Dim RowNumber As Long
    
    ' Set the file path and row number to start writing to
    PathOfFile = ThisWorkbook.Sheets(1).Range("F7").Value
    RowNumber = 1
    
    ' Open the text file for input
    F_Number = FreeFile()
    Open PathOfFile For Input As #F_Number
    
    ' Loop through each line of the file and write to the worksheet
    Do Until EOF(F_Number)
        Line Input #F_Number, Line_Txt
        ' Write the text to the worksheet
        Range("A" & RowNumber).Value = Line_Txt
        ' Increment the row number
        RowNumber = RowNumber + 1
    Loop
    
    ' Close the file
    Close #F_Number
    
End Sub

Result: After running the code, every line will be inserted in the cells of column A, as shown below.

Excel VBA to Read Text File Line by Line2

Frequently Asked Question

How do I read a specific line from a text file in VBA?

Please use the code given below to read a specific line from a text file in VBA

Sub Read_specific_line()


Dim filePath As String
Dim lineNum As Integer
Dim lineNumber As Integer
Dim lineText As String

' Set the file path and line number
filePath = "C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt"
lineNum = 5

' Open the file
Open filePath For Input As #1

' Loop through the lines until you reach the desired line
lineNumber = 1
Do Until EOF(1) Or lineNumber = lineNum
    Line Input #1, lineText
    lineNumber = lineNumber + 1
Loop

' Close the file
Close #1

' show the desired line
If lineNumber = lineNum Then
     MsgBox lineText
Else
    Debug.Print "Line not found"
End If

End Sub

How do I check each line of a text file by VBA code?

Please use the code we gave in the first portion of the article (code 1). Every time you press the OK button, the message box will show you each line to check easily.

How to Read Text (UTF-8) File line by line by VBA?

You can use the following VBA code to read UTF-8 encoded text file line by line

Dim fso As Object
Dim file As Object
Dim textStream As Object
Dim filePath As String
Dim lineText As String

' Set the file path
filePath = "C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt"

' Create a new instance of the FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")

' Get the file
Set file = fso.GetFile(filePath)

' Open the file as a TextStream
Set textStream = file.OpenAsTextStream(1, -2)

' Read the file line by line
Do While Not textStream.AtEndOfStream
    lineText = textStream.ReadLine
    ' Do something with the lineText variable, such as print it to the Immediate window
    MsgBox lineText
Loop

' Close the file and release memory
textStream.Close
Set textStream = Nothing
Set file = Nothing
Set fso = Nothing
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc
Jump to Topics
SOLVED EXCEL
Scroll to Top