Written by Allen Wyatt (last updated January 15, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11115) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Excel here: Getting Input from a Text File.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Workbooks are loaded from disk files, but workbooks aren't the only type of files that Excel can load. This tip provides ...
Discover MoreIf you get errors in Excel that your filenames are too long, it can be confusing and frustrating. Applying the ideas ...
Discover MoreExcel provides a quick way to access the workbooks you've most recently worked on. This tip addresses how to display a ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-01-16 04:06:33
Kiwerry
@Mark: While I would agree with you as far as new input applications, or, for example, in cases where the format of the are concerned, I see no point in changing a running system where an existing VBA application is performing satisfactorily.
Apart from that, VBA can be used to write text files as well as read them, which opens, for example, the possibility of exporting of a table of position data as a gpx or kml file.
2025-01-15 06:51:43
Mark
I suggest that VBA be abandoned in favor of the built-in Extract-Transform-Load (ETL) tool - PowerQuery. With it you can do all the rest of typical text file input functions like parsing things, setting up/processing date/times, etc.
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments