Astra

Excel VBA Code to Read Text File Without Opening

Estimated reading: 5 minutes

The following VBA codes will read a text file from the given path without opening the text file.

Method 01: Read the Text File using the Input method Without Opening the text file

Sub ReadTextFile_FastestWay()
    
    'Declare variables
    Dim Text_data As String
    Dim F_Path As String
    Dim F_Number As Integer
    
    'Set the file path (you have to modify the path accordingly)
    F_Path = "C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt"
    
    'Open the text file
    F_Number = FreeFile()
    Open F_Path For Input As #F_Number
    
    'Read the entire text file
    Text_data = Input$(LOF(F_Number), #F_Number)
    
    'Close the text file
    Close #F_Number
    
    'show the text data in message box
    MsgBox Text_data
    
End Sub

Note: Before running the code, please check the following references 

Excel VBA Code to Read Text File Without Opening

Result: After running the code, the text file will be shown in the message box. 

Excel VBA Code to Read Text File Without Opening1

Method 02: Read the Text File using the ADODB.Stream object 

Sub ReadTextFile_Method2()

    'Declare variables
    Dim Stream_obj As Object
    Dim Text_data As String
    Dim F_Path As String
    
    'Set the file path
    F_Path = "C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt"
    
    'Create an ADODB.Stream object
    Set Stream_obj = CreateObject("ADODB.Stream")
    
    'Configure the stream to read text
    Stream_obj.Type = 2 'Text stream type
    Stream_obj.Charset = "UTF-8"
    
    'Open the text file
    Stream_obj.Open
    Stream_obj.LoadFromFile F_Path
    
    'Read the entire text file
    Text_data = Stream_obj.ReadText
    
    'Close the text file
    Stream_obj.Close
    
    'Clean up
    Set Stream_obj = Nothing
    
    'Dispay the text
    MsgBox Text_data
    
End Sub

Method 03: Read the Text File using the File System object 

Sub ReadTextFile_Method3()

    'Declare variables
    Dim F_sys_obj As Object
    Dim T_Stream As Object
    Dim Text_data As String
    Dim F_Path As String
    
    'Set the file path
    F_Path = "C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt"
    
    'Create a FileSystemObject
    Set F_sys_obj = CreateObject("Scripting.FileSystemObject")
    
    'Open the text file
    Set T_Stream = F_sys_obj.OpenTextFile(F_Path, 1, False)
    
    'Read the entire text file
    Text_data = T_Stream.ReadAll
    
    'Close the text file
    T_Stream.Close
    
    'Clean up
    Set F_sys_obj = Nothing
    Set T_Stream = Nothing
    
    'Do something with the text
    MsgBox Text_data
    
End Sub

Method 04: Read Text File Line By Line

You are recommended to read the article VBA Code to Read the Text File Line by Line to learn about method 4.

Example: open text file and copy data to excel sheet by vba 

Sub OpenTextFileAndCopyToExcel_SaveSheet()
    
    'Declare variables
    Dim F_Path As String
    Dim F_Number As Integer
    Dim Each_Line As String
    Dim Row_Number As Long
    
    'Set the file path
    F_Path = "C:\Users\USER\Downloads\SOLVED EXCEL\A Fairy Song.txt"
    
    'Open the text file
    F_Number = FreeFile()
    Open F_Path For Input As #F_Number
    
    'Copy the data to Excel
    Row_Number = 1
    Do While Not EOF(F_Number)
        'Read a line of text from the file
        Line Input #F_Number, Each_Line
        
        'Copy the text to the Excel sheet
        Range("A" & Row_Number).Value = Each_Line
        
        'Increment the row number
        Row_Number = Row_Number + 1
    Loop
    
    'Close the text file
    Close #F_Number
    
    'Save the Excel sheet
    ThisWorkbook.Save
    
End Sub

Result: Every line of the text file will be inserted in the Excel sheet as shown below

Excel VBA Code to Read Text File Without Opening2

Frequently Asked Questions

What is the Fastest Way to Read Text Files by VBA?

According to our understanding, comparing the execution times of in-built functions in VBA, the Input function in binary mode gives the output within the shortest response time.

We used the following code to compare the execution time:

Sub CompareExecutionTime()
    
    ' Declare variables
    Dim StartTime As Double
    Dim EndTime As Double
    Dim Text As String
    Dim F_Path As String
    Dim F_Number As Integer
    
     ' Set the file path
    F_Path = "C:\Users\USER\Downloads\SOLVED EXCEL\sample-large-text-file.txt"
    
    
    ' Method 1: Input function with binary mode
    StartTime = Timer
    F_Number = FreeFile()
    Open F_Path For Binary As #F_Number
    Text = Space$(LOF(F_Number))
    Get #F_Number, , Text
    Close #F_Number
    EndTime = Timer
    'As execution time is very small to count, so we multiplied by 1000
    MsgBox "Method 1 took " & Format((EndTime - StartTime) * 1000, "#0.0000") & " milliseconds to execute."
    
    ' Method 2: Input function with text mode
    
    
    StartTime = Timer
    F_Number = FreeFile()
    Open F_Path For Input As #F_Number
    Text = Input$(LOF(F_Number), #F_Number)
    Close #F_Number
    EndTime = Timer
    'As execution time is very small to count, so we multiplied by 1000
    MsgBox "Method 2 took " & Format((EndTime - StartTime) * 1000, "#0.0000") & " milliseconds to execute."
    
   ' Method 3: Read file line by line
   
    StartTime = Timer
    F_Number = FreeFile()
    Open F_Path For Input As #F_Number
    Do While Not EOF(F_Number)
        Line Input #F_Number, Text
    Loop
    Close #F_Number
    EndTime = Timer
    'As execution time is very small to count, so we multiplied by 1000
    MsgBox "Method 3 took " & Format((EndTime - StartTime) * 1000, "#0.0000") & " milliseconds to execute."
    
End Sub

Results: 

Excel VBA Code to Read Text File Without Opening6

Note: If you use the small text file, the code may return 0 execution time. We used the following dummy text file to carry on the comparison.

2 thoughts on “Excel VBA Code to Read Text File Without Opening”

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