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

Saving Information in a Text File

Written by Allen Wyatt (last updated December 2, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


There may be times when you want a macro to save information to a text file. This is very easy to do. All you need is to open the file for output, and then start sending information to the file. The following code fragment writes a text file using this method.

Open "MyFile.Dat" For Output As #1
Print #1, NumValues
For J = 1 to NumValues
    Print #1, UserVals(J)
Next J
Close #1

The first thing written to the file is a numeric value indicating how many individual values will follow it (the code presumes that you set this value in the NumValues variable). Then a For ... Next loop is used to create the balance of the file.

You should be aware that the code, as written, will overwrite any existing MyFile.Dat file. If you want to protect the file in some way, your code will need to check to see if it exists before opening it or you'll need to set the attributes of the file to read-only.

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 (8885) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Saving Information in a 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

Understanding the For ... Next Structure

Part of the power of VBA is being able to control when some of your code executes and when it doesn't. A primary way to ...

Discover More

Hiding Errors on Printouts

If there are error values in a worksheet, you may not want those error values to appear on a printout. Excel actually ...

Discover More

Converting Individual Endnotes and Footnotes

Word makes it easy to convert all your footnotes to endnotes and vice versa. You may want to only convert a couple of ...

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)

Generating Unique Numbers for Worksheets

You may need to automatically generate unique numbers when you create new worksheets in a workbook. Here are a couple of ...

Discover More

Counting Empty Colored Cells

There are a variety of ways that you might want to count the cells in your worksheet. One way is to figure out how many ...

Discover More

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in ...

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}] (all 7 characters, in the sequence shown) 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 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.