ThisCalculator

Everyday

Excel Export: Save Each Row as a Separate Text File

Updated · Standard formula, shown below

Excel Export: Save Each Row as a Separate Text File

Yes, you can export each row of an Excel worksheet as a separate text file, but Excel has no built-in command that does this directly. You can achieve it with a VBA macro, a Power Query approach, or by using a third-party tool, and this article explains each method in detail.

Why You Might Need This

Exporting rows as individual text files is common when you need to feed data into another system that expects one file per record. Examples include:

  • Generating configuration files for devices or software.
  • Creating individual data feeds for a website or database import.
  • Splitting a large dataset into per-customer or per-order files for distribution.
  • Preparing batch uploads for an ERP or CRM system.

Doing this manually—copying each row and pasting into a new file—works for a handful of rows but becomes impractical beyond a few dozen. A scripted solution saves time and eliminates copy‑paste errors.

Method 1: VBA Macro (Most Flexible)

Visual Basic for Applications (VBA) is the most common way to automate this in Excel. The macro below loops through each row in a selected range and writes the cell values to a separate text file, using a delimiter of your choice (e.g., comma, tab, pipe).

Here is a basic macro that saves each row of the used range as a CSV file:

Sub ExportRowsToTextFiles()
    Dim ws As Worksheet
    Dim rw As Range
    Dim cell As Range
    Dim line As String
    Dim filePath As String
    Dim fileNum As Integer
    Dim i As Long
    
    Set ws = ActiveSheet
    filePath = "C:\\YourFolder\\"  ' Change this to your output folder
    
    For Each rw In ws.UsedRange.Rows
        line = ""
        For Each cell In rw.Cells
            line = line & cell.Value & ","  ' Use comma as delimiter
        Next cell
        ' Remove trailing comma
        line = Left(line, Len(line) - 1)
        
        fileNum = FreeFile
        Open filePath & "Row_" & rw.Row & ".txt" For Output As #fileNum
        Print #fileNum, line
        Close #fileNum
    Next rw
End Sub

To use it:

  1. Press Alt+F11 to open the VBA editor.
  2. Go to Insert > Module and paste the code.
  3. Change the filePath to a real folder on your computer.
  4. Run the macro (F5) while the worksheet with your data is active.

The macro writes each row as a separate file named Row_2.txt, Row_3.txt, etc., based on the row number. You can customize the file name to use a cell value (e.g., an ID column) by replacing Row_ & rw.Row with something like rw.Cells(1, 1).Value.

Method 2: Power Query (No Coding)

Power Query is a data transformation tool built into Excel (2016 and later). It can split a table into multiple tables based on a filter, but it does not directly export each row to a separate file. However, you can combine Power Query with a “parameter table” and the “Load To” feature to create separate worksheets, then export those manually or with a simple macro. This method is less automated but useful if you prefer not to write VBA.

Steps to split rows into separate sheets using Power Query:

  1. Select your data range and go to Data > From Table/Range.
  2. In the Power Query editor, add an index column (Add Column > Index Column).
  3. Create a list of unique index values (e.g., by referencing the index column).
  4. For each index value, duplicate the query and filter to that index.
  5. Load each filtered query to a new worksheet.

This is tedious for many rows, but you can automate the loading with a small VBA loop that refreshes a parameterized query. In practice, most users find VBA simpler.

Method 3: Third-Party Tools and Add-ins

Several Excel add-ins and standalone utilities offer “export each row to text file” as a one-click feature. These tools often provide a graphical interface where you select the delimiter, file naming pattern, and output folder. While convenient, they may require a purchase or a subscription. Before buying, check if the free VBA approach meets your needs—it usually does.

When choosing a third-party tool, look for:

  • Support for your Excel version (Windows or Mac).
  • Ability to choose delimiter (comma, tab, semicolon, custom).
  • File naming options (use cell values, row numbers, or a prefix).
  • Handling of special characters and line breaks within cells.

Common Problems and How to Avoid Them

Several issues can trip up an export script. Here are the most frequent and how to handle them:

  • File path errors: The folder must exist before running the macro. Add MkDir to create it if needed.
  • Special characters in data: If a cell contains a comma, a simple CSV will break. Use a delimiter that doesn’t appear in your data, or wrap fields in quotes (e.g., """" & cell.Value & """").
  • Line breaks within a cell: A cell may contain a newline character (Alt+Enter). This will create extra lines in the text file. Use Replace(cell.Value, vbLf, " ") to strip them.
  • Empty rows: The UsedRange may include rows that appear empty but have formatting. Check IsEmpty before writing.
  • File name collisions: If you use a cell value as the file name, ensure it doesn’t contain invalid characters like /\:*?"<>|. Sanitize the name with a function.

Comparing the Methods

Here’s a quick comparison to help you decide:

  • VBA Macro: Free, fully automated, works on Windows and Mac (with minor syntax differences). Requires enabling macros and trusting the code.
  • Power Query: No coding, but limited to splitting into sheets, not files. Good for one-time manual work.
  • Third-party tools: Easiest to use, but may cost money and require installation. Good for non-technical users.

For most situations, VBA is the best balance of cost and control.

Safety and Accuracy Tips

When running any export, take these precautions:

  • Back up your workbook before running a macro, especially if it modifies data.
  • Test on a small sample first (e.g., first 5 rows) to verify the output format.
  • Check the output folder after the run to ensure all files were created and named correctly.
  • Be aware of file encoding: The Open ... For Output method writes in ANSI encoding. If your data contains non‑Latin characters, you may need to use ADODB.Stream to write UTF‑8.

Frequently Asked Questions

Can I export each row as a separate CSV file without VBA?
No, Excel has no built-in feature to export multiple rows to separate files. You can manually copy each row and paste into a new file, but that is not practical for large datasets. VBA or a third-party tool is required.

How do I name each file using a cell value?
In the VBA macro, replace the file name line with something like filePath & rw.Cells(1, 1).Value & ".txt" to use the value from column A. Ensure the cell value is a valid file name.

Will this work on Excel for Mac?
Yes, VBA works on Mac, but the file path syntax is different (e.g., use "/Users/username/Folder/"). Also, some Windows‑specific functions may not be available, but the basic file operations are fine.

How can I export only visible rows after filtering?
Modify the macro to loop through ws.Rows.SpecialCells(xlCellTypeVisible) instead of UsedRange. This will skip hidden rows.

What if my data contains a comma?
If your delimiter is a comma, wrap each field in double quotes in the macro. For example: line = line & """" & cell.Value & """,". Alternatively, use a tab or pipe as the delimiter.

Related

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