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

Getting Input from a Text File

Written by Allen Wyatt (last updated September 26, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

True to its BASIC roots, VBA allows you to do file input on sequential files. This means you can open and read a sequential text file, loading the information from the file into string variables. The steps are simple. You only have to open the file, get the input, and then close the file. The following code is a common example of reading from a sequential file:

Dim Raw As String
Dim NumValues As Integer, J As Integer
Dim UserVals() As String

Open "MyFile.Dat" For Input As #1
Line Input #1, Raw
NumValues = Val(Raw)
ReDim UserVals(NumValues)

For J = 1 to NumValues
    Line Input #1, UserVals(J)
Next J
Close #1

In this example you should note that the first line read from the text file (MyFile.Dat) is assumed to contain a value that indicates how many items are to be read in from the file.

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

Weird Hyperlink Behavior

When you insert a hyperlink, you expect it to look like, well, a hyperlink. But what if it really looks like some strange ...

Discover More

Changing Existing Highlighting

Need to change the color you previously used to highlight text in your document? You can use Word's Find and Replace tool ...

Discover More

Converting Text to Values

When you import information originating in a different program, Excel may not do the best job at figuring out what ...

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)

Full Path to Workbook

Need to get the full path of your current workbook into the Clipboard? Excel doesn't make it quite as easy as it used to ...

Discover More

Excel Filename Is Too Long

If you get errors in Excel that your filenames are too long, it can be confusing and frustrating. Applying the ideas ...

Discover More

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. ...

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 6 + 5?

2020-09-26 05:12:39

Kiwerry

A quick note for those who have data files in which line 1 doesn't contain a value that indicates how many items are to be read in from the file:
Enclose the read and process code in a while ... wend loop like this
While Not EOF(1)
Line Input #1, Datensatz
' .... do stuff with the string variable Datensatz
Wend

I use this for reading gpx or html files.


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.