Save a range as text file using VBA [tutorial]

VBA Macros - 17 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:


Hello Awesome...

My name is Chandoo. Thanks for dropping by. My mission is to make you awesome in Excel & your work. I live in Wellington, New Zealand. When I am not F9ing my formulas, I cycle, cook or play lego with my kids. Know more about me.

I hope you enjoyed this article. Visit Excel for Beginner or Advanced Excel pages to learn more or join my online video class to master Excel.

Thank you and see you around.

Related articles:

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

17 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.

    • Madh says:

      Mark, That was perfect reply, Saved lot of time for me, I was searching for removing double quotes while exporting, Now just changed that "," to "." .. Thatsall!!! Thanks aLot .....

  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


    • D.J.C says:

      I create a 'master' power formula column that restructures my data to the exact output that I desire - To achieve 'x' decimals rounded....

      Example Data
      COL'A' COL'B' COL'C' COL'D' COL'E'
      TEST_LINE_03 7.655 7.655 03/03/2017 03/03/2017

      Formula in Column J
      My Numeric Data (COL'C') needs to be rounded to 2 places...


      Output Text

      I need Text to be 'Quoted', while numeric and dates needed to be without quotes.

      Hope this helps, as well as a little extra.

  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?

  7. Amol says:

    I am trying to write a code for writing excel data (with merged cells)
    to text file.
    I am not able to do it perfectly.
    Please guide me.


  8. minimalist says:

    If I want tab-delimited output what do I enter instead of the "," in this code?

  9. minimalist says:

    I found vbTab as "tab" character.

    Can we modify this code so it uses the current selection of cells as the range to save as text? For example selecting the columns Name and Date of Join and running this code should save only these 2 columns to text:

    Name,Date of Join
    Adam Ackert,13-04-2010


  10. beebee says:

    is it possible to keep the cell format in the output ie. dollar symbol etc?

  11. NAC says:

    Thanks a lot for this post! But how can define in VBA that the output file has to be "UTF-8"?. Right now it is being created in ANSI and i have problems with special characters as áí and so on. thanks in advance

  12. Gert says:


    I want to use this as a backup/log textfiles. Is there also a way to place the textfile back into a sheet as text ( no formating ) in VBA ?
    So overwriting the incomplete or missing cells.

    Could not find yet.

  13. Andy says:

    Hi there,

    Is there a way I can get the filename to reference a cell instead of the time and date stamp?

    filename = ThisWorkbook.Path & "\textfile-" & Format(Now, "ddmmyy-hhmmss") & ".txt"

  14. kun says:

    can you explain about this code for me.
    lineText = IIf(j = 1, "", lineText & ",") & myrng.Cells(i, j)
    i was not clear about it

Leave a Reply

« »