How to Read Data From Text File in Excel VBA
Last Updated :
19 Sep, 2023
VBA Program to read a Text file line by line (Sales Data) and place it on a worksheet It provides a concise introduction to the process of importing data from text files into Microsoft Excel using Visual Basic for Applications (VBA). This introductory article serves as a starting point for individuals who want to automate the task of reading and processing data from external text files within an Excel workbook. It likely covers essential concepts such as variable declarations, file handling, and data extraction, setting the stage for readers to delve deeper into the specifics of VBA coding to accomplish this task efficiently. Overall, it offers readers a fundamental understanding of the topic and hints at the practical benefits of utilizing VBA for data manipulation in Excel.
Sales Data in Text File: 5 Fields [ Product, Qtr 1, Qtr 2, Qtr 3 and Qtr 4 ] and 25 Records (incl. header)
Sales dataVBA code will read a text file and place it on worksheet cells as below

VBA - Read Text File Line by Line
Declaring variables
|
line | String | Read text file line by line |
Filename | String | Input file name (Full path) |
i | Integer | Iterator |
value() | String | split the sentence by comma and store it in an array variable of type String |
'Variable declarations
Dim line As String, Filename As String, i As Integer, valueArr() As String
Initialize the "Filename" variable with full path and filename
'Text file fullPath
Filename = "D:\Excel\ReadTextFile\sales.txt" 'update your full file path
i = 1
Open the input file to read the text
'Open file
Open Filename For Input As #2
Read input file line by line
'Read line by line - text file
While Not EOF(2)
Line Input #2, line
- Split by a comma and store it in valuer (). In our example, each line has 5 values concatenated with a comma.
'split the line by comma separated, assigned in an array
valuesArr() = Split(line, ",")
- Add text to respective cells from values (). Read each item in an array by its index value
Cells(i, "A").Value = valuesArr(0)
Cells(i, "B").Value = valuesArr(1)
Cells(i, "C").Value = valuesArr(2)
Cells(i, "D").Value = valuesArr(3)
Cells(i, "E").Value = valuesArr(4)
Increment counter i, to move next line.
i = i + 1
Wend
Close file
'Close file
Close #2
How to Read Text File Line by Line
Step 1: Open Excel
Step 2: Add a Shape
Add a shape (Read Text File) to your worksheet.

Step 3: Right Click and Select Assign Macro
Right-click on “Read Text file” and “Assign Macro..”

Step 4: Select ReadTextFileLineByLine Macro

Step 5: Save your Excel File
Save your Excel file as “Excel Macro-Enabled Workbook” *.xlsm

Step 6: Click Read Text file

Step 7: Adjust the Column Width
Adjust the column width in your Excel file.

How to Read Text Files in Arrays in Excel
In Excel VBA, you can read text files into arrays by following these steps:
Step 1: Open Excel and Press ALT + F11 to Open the VBA Editor
Step 2: Insert a New Module
Insert a new module by clicking "Insert" > "Module."
Insert a Module
Step 3: Enter the Below code in VBA window
Note: Please replace "C:\YourFolderPath\YourFile.txt" with the actual path of the text file you want to read. This code will read the lines from the text file and store them in the TextArray. It also includes an example of how to print the contents of the array to the Immediate Window for testing purposes. You can modify the code to perform any desired operations on the data stored in the array.
Step 4: Now Click on Run Module
Similar Reads
How to read excel file in R ? We may work with structured data from spreadsheets, take advantage of R's capabilities for data analysis and manipulation, and incorporate Excel data into other R processes and packages by reading Excel files in R. The readxl package offers a simple and effective method for reading Excel files into
3 min read
R Read Text File to DataFrame In today's data-driven world, collecting data from multiple sources and turning it into a structured manner is a critical responsibility for data analysts and scientists. Text files are a prominent source of data, as they frequently include useful information in plain text format. To be used success
5 min read
How to Run Code from a Module in Excel VBA VBA Macro is for developers. In Excel, the macro is a piece of code written in VBA and VBA is Microsoft's programming language, it stands for Visual Basic for Applications. The module is a file with a .bcf extension that stores the code written in the Visual Basic for Applications editor. Let's lear
4 min read
How to plot data from a text file using Matplotlib? Perquisites: Matplotlib, NumPy In this article, we will see how to load data files for Matplotlib. Matplotlib is a 2D Python library used for Date Visualization. We can plot different types of graphs using the same data like: Bar GraphLine GraphScatter GraphHistogram Graph and many. In this article,
3 min read
How to Use Find and Replace Text and Numbers in Excel Have you ever spent hours scrolling through rows in Microsoft Excel, searching for that one typo or outdated number buried in your spreadsheet? Youâre not alone. Manually hunting for data is time-consuming and error-proneâbut what if you could fix mistakes or update information in seconds? Thatâs wh
7 min read
How to Read Text Files with Pandas? In this article, we will discuss how to read text files with pandas in Python. In Python, the Pandas module allows us to load DataFrames from external files and work on them. The dataset can be in different types of files.Text File UsedRead Text Files with PandasBelow are the methods by which we can
6 min read
How to import an Excel File into R ? In this article, we will discuss how to import an excel file in the R Programming Language. There two different types of approaches to import the excel file into the R programming language and those are discussed properly below. File in use: Method 1: Using read_excel() In this approach to import th
3 min read
How to Use the VBA Editor in Excel: Quick Guide 2024 Unlock the full potential of Microsoft Excel by diving into the world of Visual Basic for Applications (VBA). The VBA Editor in Excel is a powerful tool that allows you to automate tasks, create custom functions, and streamline your workflow like never before. Whether you're looking to boost product
7 min read
How to Open a CSV File in Google Sheets Ever received a data dump in a mysterious file format called CSV? Don't worry, it's not an alien language! CSV stands for "Comma-Separated Values," and it's a common way to store data in rows and columns. But how do you unlock this treasure trove of information? Fear not, data warrior! This guide wi
5 min read
How to Use AI in Excel for Automated Text Analysis? Text analysis is a machine learning technique used to automatically extract valuable insights from unstructured text data. Companies use text analysis tools to quickly digest online data and documents, and transform them into actionable insights. We can use text analysis to extract specific informat
2 min read