Save a range as text file using VBA [tutorial]

Posted on October 28th, 2015 in VBA Macros - 6 comments

Last night I asked members of our facebook page to share an Excel problem you are struggling with.  Francis asked,

How to save a file as .txt in vba without quotes? When I save as .txt, the file has got quotes inside of it. I used the code Print, but it didnt work because the file loses its delimitation.
Does anyone know how to solve this?

Let’s understand how to save a range as text and overcome the double quote problem.

Saving a range as Text – the easy way

Say you have a file like this:


The easy option is to save your worksheet as text file using below macro.

Sub saveText()
    ActiveWorkbook.SaveAs filename:= _
        ThisWorkBook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt", FileFormat:=xlText, _
End Sub

While this works, it has 2 problems.

  1. It exports the entire current worksheet.
  2. It adds double quotes “” to text values or formatted cells.

So you get this.


Saving a range as Text – the proper way

We can create a blank text file using VBA and write the range data values in to that file. This way we will have full control over what goes in to the file and how it’s formatted.

Here is the code:

Sub saveText2()
    Dim filename As String, lineText As String
    Dim myrng As Range, i, j
    filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"
    Open filename For Output As #1
    Set myrng = Range("data")
    For i = 1 To myrng.Rows.Count
        For j = 1 To myrng.Columns.Count
            lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)
        Next j
        Print #1, lineText
    Next i
    Close #1
End Sub

Let’s understand the code…

Create a file name

We take the current workbook path and set up textfile-time stamp.txt in that directory.

Note, the time stamp portion is dynamic and changes every time you run the code.

We then open the file using Open filename For Output As #1 line.

This sets up a new file and opens it for us to write anything we want.

Loop thru range data and write values to the file

We loop thru each and every cell of the range("data"). We need to take all the values in a row and concatenate them with delimiter comma (,).

This is done in nested for loops (related: Introduction to For loop- Excel VBA)

We loop thru each column in a row and construct lineText.

We then print this lineText to file #1 using,

Print #1, lineText

Finally we close the file.

The end result

This is what we get.


Download Save Text example macro

Click here to download the example workbookExamine the savetext macro to learn more.

How do you create text files using VBA?

Do you create text / CSV / TSV files from Excel data? How do you automate the process? Please share your tips and ideas in the comments section.

Learn more powerful ways to use VBA:

Written by Chandoo
Tags: , , , , , , ,
Home: Main Page
? Doubt: Ask an Excel Question

6 Responses to “Save a range as text file using VBA [tutorial]”

  1. Graham says:

    I export data to an xml file using VBA rather than using the built in Excel "Export to XML" . Using VBA, MSXML and XSLT, the data can then be transformed into other datasets and html pages without the extra HTML tags and styles that are added to a file when using the Excel option of "Save as Web Page".

  2. mark says:

    the double quotes are present only because there are commas in the data. Remove them (for a test) and they are not present. The problem with this solution is that if you really need the commas and you save the text with them, but are not able to differentiate between commas as data and comma as delimiter, the structure of the data is not recoverable. if i were writing a program to read files like this (and i have, often using Rexx), i'd use tabs or commas, accept the quotes, and strip. Still quite easy.

  3. Srinivasan says:

    Print command works for me.
    Any how let me check up

  4. Niclas says:

    Im using the code, but need help on how to print the values with 4 decimals roundup


  5. Srinivasan Venkatraman Srinivasan says:

    So good

  6. Havoc says:

    Awesome code... Is there a way to save the text file in a protected format to prevent editing?

Leave a Reply