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

Flipping Data

Have you ever spent a lot of time putting information into a worksheet, only to realize that you should have put it in ...

Discover More

Printing Only Changed Pages

Turn Track Changes on, and you can easily see where you've made changes throughout a document. If you want to print only ...

Discover More

Changing the Reference in a Named Range

Define a named range today and you may want to change the definition at some future point. It's rather easy to do, as ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

File Formats that Include Field Formats

If you import data into Excel that is created by other programs, you know that it can be bothersome to get your data ...

Discover More

Opening a Workbook with Two Windows

If you open a workbook and notice that Excel displays two windows for it, this has to do with how the workbook was saved. ...

Discover More

Importing Multiple Files to a Single Workbook

If you use Excel to work with data exported from another program, you might be interested in a way to import a large ...

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 9 + 2?

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.