Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Appending to a Non-Excel Text File.

Appending to a Non-Excel Text File

by Allen Wyatt
(last updated June 23, 2016)

4

When using a macro to write information to a text file, you may want to add information to an existing file, rather than creating a new text file from scratch. To do this, all you need to do is open the file for Append rather than Output. The following code shows this process:

Open "MyFile.Dat" For Append As #1
For J = 1 to NewValues
    Print #1, UserVals(OrigVals + J)
Next J
Close #1

When the file is opened for Append mode, any new information is added to the end of the file, without disturbing the existing contents.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9206) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Appending to a Non-Excel Text File.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Using Graphics to Represent Data Series

You can spice up your bar chart by using a graphic, of your choosing, to construct the bars. This tip shows how easy it ...

Discover More

Finding Related Words

One part of the grammar tools provided with Word is a thesaurus that helps you find all sorts of word variations. One ...

Discover More

Recovering Macros from Corrupted Workbooks

Workbooks get corrupted from time to time; that's a fact of life in an Excel world. If those corrupted workbooks contain ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Who Has the File Open?

Open a workbook that someone else is working on, and you won't be able to save your changes back into the same file. ...

Discover More

Avoiding Scientific Notation on File Imports

When importing information from a CSV file, you may get unintended results from time to time. Here's how to force Excel ...

Discover More

Saving in Two Locations

When you save a workbook to disk, you may want to automatically save a duplicate workbook in a separate location. This ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 3?

2016-06-23 09:28:22

allen@sharonparq.com

Sure. Here you go:

Open "MyFile.Dat" For Append As #1
Print #1, "Hello World"
Close #1

-Allen


2016-06-23 07:24:49

Lisa Smith

I'm a little confused by your code sample

Could you give an example that would append "Hello World" to a text file?

Lisa


2013-12-16 11:23:48

Duncan

Ref Barry's comment aboutr REDIM being slow, would it not be possible to start by doing a "large" Redim to provide plenty of spare space in the array (for instance doubling its size), then keep track of how many items have been added so that you can do another big Redim when needed, etc. Then at end of file, when you know the total number of items, you do a final Redim down to the actual number needed (or if that isn't allowed, copy the filled array elements into a new array of the correct size and delete the original). or you could just leave the empty elements.


2013-12-14 07:03:03

Barry Fitzpatrick

Please this is not compatible with companion tips for creating and reading back text file

http://excelribbon.tips.net/T008885_Saving_Information_in_a_Text_File.html

http://excelribbon.tips.net/T011115_Getting_Input_from_a_Text_File.html

This is because the Append doesn't update the first entry which indicates the total number of lines of data in the file, and then when inputting the data from the file the appended data will be ignored.

Using the EOF function obviates the need to have the number of lines parameter at all. As the total number of lines is unknown the array will have to be Redimensioned for each line inputted (which can be slow, and isn't terribly efficient) using the "Preserve" parameter. Or read the file twice, once to count the number of lines, and so dimension the array, and the second time to populate the array with the read data.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.