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.

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

Establishing a FLOOR and CEILING

Excel includes a surprising number of functions you can use to round your data. Two such functions are FLOOR and CEILING, ...

Discover More

Monday's Date on Friday's Report

Automatically putting today's date at the top of a report is easy. Putting a different date, automatically, can be more ...

Discover More

Getting Notified of Spreadsheet Changes

Google Sheets makes it easy to receive notifications by e-mail whenever anything in a spreadsheet changes. Here's how to set ...

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)

Getting Rid of Extra Quote Marks in Exported Text Files

If you don't like the way that Excel exports information you intend to use with other programs, then your best bet is to ...

Discover More

MRU Files Won't Display

Excel provides a quick way to access the workbooks you've most recently worked on. This tip addresses how to display a list ...

Discover More

Determining If a File Exists

Before you have your macro open and read a file from disk, you'll want to check to make sure it is really there. Here's how ...

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 3 + 8?

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.