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

Printing a Process List

There are programs (processes) running in your system all the time. You can view a list of these processes using the Task ...

Discover More

Filling Cells with Decreasing Cell References

AutoFill is a great feature. It can detect patterns and adjust cell contents as you drag a selection on-screen. It doesn't ...

Discover More

Word 2010 Terrific Tables (Table of Contents)

Word allows you to create and format tabular information using a powerful table editor. Discover the many ways that you can ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros 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. Wouldn't ...

Discover More

Grabbing the MRU List

Excel keeps track of the most recent workbooks you've used. If you want to access that information in a macro, you'll need ...

Discover More

Finding the Parent Folder

Do you need to figure out the name of the parent folder of whatever folder a worksheet is in? Believe it or not, this can be ...

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 7 - 0?

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.