How to Import a Text File to Excel Using VBA (3 Effective Ways)

Table of Contents

Table of Contents

Importing a Text File to Excel VBA means using Visual Basic for Applications (VBA) code to bring external text data into Excel worksheets automatically. Instead of using manual import wizards, VBA allows users to automate the process, define delimiters, and handle repetitive imports efficiently. We use VBA because it helps to save time, reduce manual effort, and ensure data consistency, particularly when we need to handle multiple text files and structured reports.

Key Takeaways

To import a text file to Excel using VBA, follow these steps:

➤ Open a new or existing Excel workbook.
 Alt  +  F11  to open the VBA editor.
➤ Insert a new module (Insert → Module) and paste your VBA code for importing the text file.
➤ Press  F5  to run the script, select your text file from the file dialog, and the data will be imported automatically into the active worksheet.

overview image

In this article, we will describe how to import a text file into Excel VBA from a specific file location, using a file selection dialog, and importing data with multiple delimiters.

Download Practice Workbook
1

Import Text File to Excel Using VBA from a Specific File Location

VBA (Visual Basic for Applications) can import a text file directly into Excel by defining its file path. We use this approach when our text file’s location doesn’t change, such as when importing monthly or weekly reports from a fixed folder.

We have a text file that represents the sales report of an electronics shop, saved as a comma-separated text file. We will import this data into Excel by specifying the exact location of the text file using VBA.

Steps:

➤ We have taken a text file named SalesData.txt that contains comma-separated data like Product Name, Units sold, and Revenue.

Import Text File to Excel Using VBA from a Specific File Location

➤ Go to the folder where your text file is saved: StoreSalesData.txt.
➤ Hold down the Shift key, right–click on the file, and select Copy as path. This will copy the full directory of your text file, which we will later use in our VBA code.

Import Text File to Excel Using VBA from a Specific File Location

If the Developer tab is not visible, click File.

Import Text File to Excel Using VBA from a Specific File Location

Choose more → Options.

Import Text File to Excel Using VBA from a Specific File Location

Click on the Customize Ribbon.

Import Text File to Excel Using VBA from a Specific File Location

Check Developer, and click OK.

Import Text File to Excel Using VBA from a Specific File Location

Now, open a blank worksheet.
Go to the Developer tab.

Import Text File to Excel Using VBA from a Specific File Location

Then select Visual Basic to open the VBA editor.

Import Text File to Excel Using VBA from a Specific File Location

Inside the VBA editor, click Insert → Module.

Import Text File to Excel Using VBA from a Specific File Location

A new code window will open where you will paste your VBA script.

Import Text File to Excel Using VBA from a Specific File Location

➤ Copy and paste the following VBA code into the module window:

Sub ImportTextFileToExcel()
Dim textFileNum As Integer
Dim rowNum As Integer, colNum As Integer
Dim textFileLocation As String, textDelimiter As String, textData As String
Dim tArray() As String, sArray() As String
Specify the text file location
textFileLocation = "D:\StoreData\StoreSalesData.txt"
textDelimiter = ","
'Open the text file and read its data
textFileNum = FreeFile
Open textFileLocation For Input As textFileNum
textData = Input(LOF(textFileNum), textFileNum)
Close textFileNum
'Split data into rows and columns
tArray() = Split(textData, vbLf)
For rowNum = LBound(tArray) To UBound(tArray) - 1
If Len(Trim(tArray(rowNum))) <> 0 Then
sArray = Split(tArray(rowNum), textDelimiter)
For colNum = LBound(sArray) To UBound(sArray)
ActiveSheet.Cells(rowNum + 1, colNum + 1) = sArray(colNum)
Next colNum
End If
Next rowNum
MsgBox "Data Imported Successfully", vbInformation
End Sub
Explanation
➥ The textFileLocation variable stores the path of your text file.
textDelimiter = "," tells VBA that the file is comma-separated.
➥The loop structure reads each line, splits it into parts using commas, and places the data into Excel cells.

Import Text File to Excel Using VBA from a Specific File Location

Select the file path in the code.

Import Text File to Excel Using VBA from a Specific File Location

➤ Replace the file path in the code with your own (that you have copied earlier). For example: “D:\New folder (7)\SalesData.txt”. Make sure the path includes the .txt file extension.
Press  F5  on your keyboard or click Run.

Import Text File to Excel Using VBA from a Specific File Location

After the script executes, Excel will display a message box saying “Data Imported Successfully”. Click Ok.

Import Text File to Excel Using VBA from a Specific File Location

The data will appear in your active sheet.

Import Text File to Excel Using VBA from a Specific File Location


2

Import a Text File to Excel Using a File Selection Option

Data can be imported from any text file that is located anywhere into Excel by simply selecting the file through a file dialog box. This approach is suitable when the file path changes or when importing multiple files.

We have a text file that contains a shop’s sales record. The text file represents a small shop’s daily sales data containing product names, units sold, and revenue. We will import the data directly into Excel using the VBA method, without needing to manually open and copy the text file content or to paste the file location URL.

Steps:

Open a new blank worksheet. Go to the Developer tab.

Import a Text File to Excel Using a File Selection Option

➤ Then select Visual Basic to open the VBA editor.

Import a Text File to Excel Using a File Selection Option

In the VBA window, navigate to the menu and click Insert → Module.

Import a Text File to Excel Using a File Selection Option

A new code window will open where you will paste our VBA script.

Import a Text File to Excel Using a File Selection Option

Copy and paste the following VBA script inside the newly created module, which will open a file explorer window where you can select the text file to import:

Sub ImportTextFileToExcel()
Dim textFileNum, rowNum, colNum As Integer
Dim textFileLocation, textDelimiter, textData As String
Dim tArray() As String
Dim sArray() As String
textFileLocation = Application.GetOpenFilename()
textDelimiter = ","
textFileNum = FreeFile
Open textFileLocation For Input As textFileNum
textData = Input(LOF(textFileNum), textFileNum)
Close textFileNum
tArray() = Split(textData, vbLf)
For rowNum = LBound(tArray) To UBound(tArray) - 1
If Len(Trim(tArray(rowNum))) <> 0 Then
sArray = Split(tArray(rowNum), textDelimiter)
For colNum = LBound(sArray) To UBound(sArray)
ActiveSheet.Cells(rowNum + 1, colNum + 1) = sArray(colNum)
Next colNum
End If
Next rowNum
MsgBox "Data Imported Successfully", vbInformation
End Sub

Import a Text File to Excel Using a File Selection Option

Press  F5  or click the Run button in the VBA toolbar.

Import a Text File to Excel Using a File Selection Option

➤ A file explorer window will appear.

Import a Text File to Excel Using a File Selection Option

➤ Browse to your folder, select your text file. Example, SalesData.txt, and click Open.

Import a Text File to Excel Using a File Selection Option

A message box saying “Data Imported Successfully” will appear.
Click OK.

Import a Text File to Excel Using a File Selection Option

Now, the text data will be displayed in an Excel sheet.

Import a Text File to Excel Using a File Selection Option


3

Import Text File with Multiple Delimiters into an Excel Worksheet

We can import a text file that contains data separated by multiple delimiters (like commas, semicolons, or tabs) into Excel using VBA. This is efficient when our text file isn’t consistently formatted, for example, some values are separated by commas, others by semicolons.

We have a text file that represents employee details, where the text file has mixed delimiters (commas and semicolons). We will use a VBA code to successfully unify them and import the data neatly into Excel columns.

Steps:

➤ We have taken a text file named employee_data.txt that contains comma- and semicolon-separated data like Employee Name, Department, and Salary.

Import Text File with Multiple Delimiters into an Excel Worksheet

Open a blank worksheet. Go to the Developer tab.

Import Text File with Multiple Delimiters into an Excel Worksheet

➤ Then select Visual Basic to open the VBA editor.

Import Text File with Multiple Delimiters into an Excel Worksheet

Inside the VBA editor, click Insert → Module.

Import Text File with Multiple Delimiters into an Excel Worksheet

A new code window will open where you will paste your VBA script.

Import Text File with Multiple Delimiters into an Excel Worksheet

➤ Copy and paste the following code into the module window:

Sub ImportTextFileWithMultipleDelimiters()
Dim textFileNum As Integer
Dim rowNum As Integer, colNum As Integer
Dim textFileLocation As String
Dim textData As String, textDelimiter As String
Dim tArray() As String, sArray() As String
'Open File Explorer to select the text file
textFileLocation = Application.GetOpenFilename("Text Files (*.txt), *.txt", , "Select Text File to Import")
'Read data from the text file
textFileNum = FreeFile
Open textFileLocation For Input As textFileNum
textData = Input(LOF(textFileNum), textFileNum)
Close textFileNum
'Handle multiple delimiters (replace; and tab with,)
textData = Replace(textData, ";", ",")
textData = Replace(textData, vbTab, ",")
textDelimiter = ","
'Split data into rows and columns
tArray = Split(textData, vbLf)
For rowNum = LBound(tArray) To UBound(tArray) - 1
If Len(Trim(tArray(rowNum))) <> 0 Then
sArray = Split(tArray(rowNum), textDelimiter)
For colNum = LBound(sArray) To UBound(sArray)
ActiveSheet.Cells(rowNum + 1, colNum + 1).Value = Trim(sArray(colNum))
Next colNum
End If
Next rowNum
MsgBox "Data Imported Successfully!", vbInformation
End Sub
Explanation
➥ The code opens a File Explorer window for selecting a .txt file.
➥ It replaces semicolon (;) and tab with a comma (,) — converting all delimiters into one type.
➥ The data is then split into rows and columns and placed in your active Excel sheet.

Import Text File with Multiple Delimiters into an Excel Worksheet

Press  F5  or click the Run button in the VBA editor.

Import Text File with Multiple Delimiters into an Excel Worksheet

➤ A file explorer window will appear.

Import Text File with Multiple Delimiters into an Excel Worksheet

Browse and select your .txt file containing multiple delimiters (e.g., commas, semicolons, tabs). Click Open.

Import Text File with Multiple Delimiters into an Excel Worksheet

You will see a confirmation message: “Data Imported Successfully!”

Import Text File with Multiple Delimiters into an Excel Worksheet

After selecting the file, the script will import and organize the text file data into Excel columns automatically.

Import Text File with Multiple Delimiters into an Excel Worksheet


Frequently Asked Questions

Can I import a tab-delimited text file using VBA?

Yes, you can specify Tab:=True or use Delimiter:=vbTab in your VBA code to handle tab-separated files.

Where should I store my text files before importing?

Keep all your text files in a dedicated folder and use the folder path in your VBA script to automate imports.

Can I import multiple text files at once?

Yes. You can loop through all files in a folder using VBA’s Dir function to import them one by one.

What if my text file has headers or blank rows?

You can modify the VBA code to skip the first line or clean up blank rows after importing.


Concluding Words

The VBA Import Method provides a fast and flexible way to bring text data into Excel automatically. Through using simple VBA commands, you will be able to import text files to Excel VBA from a specific file location, a file selection dialog, and import data with multiple delimiters. Also, you can download the text files and datasets we have used in this article to practice on your own.

Facebook
X
LinkedIn
WhatsApp
Picture of Shihab Shahriar

Shihab Shahriar

Md. Shihab Uddin holds a Graduation in Crop Science and Technology and is pursuing a Postgraduate degree in Soil Science from the University of Rajshahi. With 4+ years of Excel and Google Sheets experience, he specializes in formulas, data cleaning, lookups, automation, VBA, formatting, and file management. He has authored 100+ in-depth Excel articles and is skilled in Power Automate, RPA, and Python. He enjoys creating efficient workflows and solving real-world data problems.
We will be happy to hear your thoughts

      Leave a reply

      Excel Insider
      Logo