ThisCalculator

Everyday

Excel Import Text File with Name: How To

Updated · Standard formula, shown below

Average pace 15.00 min/km
Average speed
4.00 km/h
Result
Total minutes
150 min
You entered

To import a text file into Excel while keeping the file name visible, use the built-in Get Data or From Text/CSV command, then add a column with the file name using Power Query or a simple formula. This article explains the exact steps, how to handle different text file formats, and common pitfalls like encoding and delimiter issues.

Formula

The calculator above is a placeholder for this article’s topic; the actual Excel import process uses no formula. For the pace/speed calculator shown, the formulas are:

  • Average pace (min/km) = Total minutes ÷ Distance (km)
  • Average speed (km/h) = Distance (km) ÷ (Total minutes ÷ 60)

Variables: t = total minutes, d = distance in km. The first output (pace) uses the formula t / d, and the second (speed) uses d / (t / 60).

Worked Example

Using the default values: t = 150 minutes, d = 10 km.

  1. Pace = 150 ÷ 10 = 15.00 min/km
  2. Speed = 10 ÷ (150 ÷ 60) = 10 ÷ 2.5 = 4.00 km/h

So the average pace is 15 minutes per kilometer, and the average speed is 4 km/h.

Why Importing a Text File with a Name Matters

When you import a text file into Excel, the file name itself often contains useful information—like a date, a batch number, or a customer ID. By default, Excel only brings in the data from the file, not the file name. If you need to track which file each row came from, you must explicitly add that name as a column. This is common when combining multiple files into one worksheet or when auditing data sources.

Excel offers two main pathways: the legacy Text Import Wizard (available in Excel 2013 and earlier, and still in newer versions via a setting) and the modern Get Data (Power Query) interface. Both can import .txt, .csv, and .prn files, but they differ in how you can capture the file name.

How to Import a Text File with the File Name in Excel

Here are the steps for the most common methods:

Method 1: Using Get Data (Power Query) – Recommended

  1. Go to the Data tab and click Get Data → From File → From Text/CSV.
  2. Select your text file and click Import.
  3. In the preview window, check that the delimiter and data types look correct, then click Transform Data (not Load).
  4. In Power Query Editor, go to Add Column → Custom Column.
  5. Enter a name like “SourceFile” and use the formula: = Source.Name (this returns the file name without the path).
  6. Click OK, then Close & Load to bring the data into a worksheet.

This method automatically includes the file name for every row. If you import multiple files from a folder, you can use the From Folder option and add the Source.Name column similarly.

Method 2: Using the Text Import Wizard (Legacy)

  1. Go to File → Options → Data and check “From Text (Legacy)” under “Show legacy data import wizards.” Click OK.
  2. Go to Data tab → Get Data → Legacy Wizards → From Text (Legacy).
  3. Select the file and go through the wizard, choosing the delimiter (e.g., comma, tab) and column formats.
  4. After the data loads, insert a new column and type a formula like =“filename.txt” (hardcoded) or use a helper cell with the file name.

The wizard does not automatically add the file name; you must do it manually. For a dynamic solution, you can use a formula that references the file path, but it’s messy.

How to Choose the Right Import Method

Your choice depends on your Excel version and how often you repeat the task:

  • Excel 2016 or later (Microsoft 365): Use Get Data (Power Query) – it’s more robust and lets you add the file name easily.
  • Excel 2013 and earlier: Use the Text Import Wizard, but be prepared to add the file name manually.
  • Mac users: Excel for Mac has a different menu structure; look for Data → Get Data (or From Text in older versions).
  • One-time import vs. recurring: For a one-off, the wizard is fine. For recurring imports, Power Query is better because you can save the query and refresh it.

Common Problems and How to Fix Them

  • File name not showing: If you used the wizard, you must add it manually. In Power Query, ensure you use Source.Name (not Source which gives the full path).
  • Wrong delimiter: If your data appears in one column, re-import and specify the delimiter (comma, tab, semicolon, or custom).
  • Encoding issues (garbled text): In the import preview, choose the correct file origin (e.g., UTF-8, Windows-1252). For Power Query, change the “File Origin” dropdown.
  • Large files slow down Excel: Use Power Query to filter and clean data before loading.
  • Leading zeros lost: In the wizard, select each column as “Text” to preserve zeros.

Alternatives to Importing with a Name

If you don’t need the file name as a column, you can simply import the data and rely on the worksheet tab name or a separate cell. Another option is to use a formula like =CELL("filename",A1) to get the full path of the current workbook, but that only gives the workbook name, not the source text file. For multiple files, consider using a folder-based approach in Power Query, which automatically adds a column with the file name.

Frequently Asked Questions

How do I import a text file into Excel without losing formatting?

Use the Text Import Wizard and set each column’s data format (e.g., Text, Date, General). This prevents Excel from auto-converting numbers like ZIP codes or IDs.

Can I import multiple text files at once and keep each file name?

Yes. In Power Query, use Get Data → From File → From Folder, then combine files. Add a custom column with Source.Name to retain each file’s name.

Why does my imported CSV show data in one column?

This usually means the delimiter is not recognized. In the import preview, check the delimiter option (comma, semicolon, tab) and ensure it matches your file. For CSV files with commas inside quoted fields, use the “Comma” delimiter and enable “Text qualifier” as double quotes.

How do I import a text file into Excel on a Mac?

In Excel for Mac, go to Data → Get Data → From Text/CSV (or From Text in older versions). The steps are similar to Windows, but the menu names may differ slightly.

What is the difference between .txt and .csv files?

A .txt file can use any delimiter (tab, comma, space) and may have no delimiter at all. A .csv file specifically uses commas to separate values. Excel treats them similarly during import, but you must specify the correct delimiter.

Can I import a text file into Excel using a formula?

No, Excel formulas cannot read external text files directly. You must use the import commands (Get Data or Text Import Wizard) or write a VBA macro.

Related

Formula used
in t | Total minutes | min | 150 | 0
out pace | Average pace | min/km | t / 10 | 2
out speed | Average speed | km/h | 10 / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.