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.
Written by Allen Wyatt (last updated September 26, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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 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.
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!
Keeping tabs on the size of a workbook can be important when using Excel. You have a couple of options that will allow ...
Discover MoreIf you try to open a second workbook and you see an error message, it could be because of the way you are opening the ...
Discover MoreImport data into a worksheet (or paste it there) and you may find that you end up with a group of blank cells you need to ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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 © 2024 Sharon Parq Associates, Inc.
Comments