Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. 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, 2013, 2016, 2019, and Excel in Office 365. 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

Exiting a For ... Next Loop Early

If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can ...

Discover More

Using Less Paper on Printouts

If a worksheet contains nothing but a bunch of values in column A, you may be loathe to print the worksheet and "waste" a ...

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

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Aligning Cells when Importing from CSV

When you import information from a CSV text file, Excel formats the data according to its default settings. Wouldn't it ...

Discover More

Creating a CSV File

Need to get your data into a format that can be easily read by other programs? Chances are good that a simple CSV file ...

Discover More

Determining the Length of a Text File

When processing plain text files in a macro, it is often helpful to know how much data the file contains. The normal way ...

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 nine more than 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.