Table of Contents
Opening a text file using VBA (Visual Basic for Applications) can be achieved by utilizing the built-in “Open” command. This command allows the user to specify the file path and access mode, such as read or write, and then manipulate the file’s contents using VBA code. Additionally, the “FileSystemObject” library can also be used to open and read text files in a more versatile manner. By using these methods, users can easily open and work with text files in VBA for various purposes, such as data manipulation and analysis.
Open a Text File Using VBA (With Example)
You can use the OpenTextFile method in VBA to open a text file from a specific file path.
Here is one common way to use this method in practice:
Sub ReadTextFile()
Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
'specify path to text fileSet MyTextFile = FSO.OpenTextFile("C:UsersbobDesktopMyTextFile.txt", ForReading)
'open text file and display contents in cell A1
TxtString = MyTextFile.ReadAll
MyTextFile.Close
ThisWorkbook.Sheets(1).Range("A1").Value = TxtString
End Sub
This particular macro reads the text file called MyTextFile.txt located on the Desktop of my computer and displays the contents of the file in cell A1.
The following example shows how to use this syntax to read a text file in practice.
Example: How to Open a Text File Using VBA
Suppose we have a text file called MyTextFile.txt located on the Desktop that we’d like to read into Excel using VBA.
Here are the contents of the file:

Before using VBA to read this file, we need to first enable Microsoft Scripting Runtime within the VB Editor.
To do so, open the VB Editor, then click Tools, then click References:

In the new window that appears, scroll down until you see Microsoft Scripting Runtime and check the box next to it. Then click OK.

Next, we can create the following macro to read a text file:
Sub ReadTextFile()
Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
'specify path to text fileSet MyTextFile = FSO.OpenTextFile("C:UsersbobDesktopMyTextFile.txt", ForReading)
'open text file and display contents in cell A1
TxtString = MyTextFile.ReadAll
MyTextFile.Close
ThisWorkbook.Sheets(1).Range("A1").Value = TxtString
End SubOnce we run this macro, the contents of the text file called MyTextFile.txt will be displayed in cell A1:

Notice that the contents of cell A1 match the contents from the text file.
Note: You can find the complete documentation for the OpenTextFile method .
Cite this article
stats writer (2024). How can I open a text file using VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-open-a-text-file-using-vba/
stats writer. "How can I open a text file using VBA?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-open-a-text-file-using-vba/.
stats writer. "How can I open a text file using VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-open-a-text-file-using-vba/.
stats writer (2024) 'How can I open a text file using VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-open-a-text-file-using-vba/.
[1] stats writer, "How can I open a text file using VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I open a text file using VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
