{"id":14129,"date":"2017-08-28T15:19:46","date_gmt":"2017-08-28T15:19:46","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=14129"},"modified":"2017-09-20T06:53:11","modified_gmt":"2017-09-20T06:53:11","slug":"vba-tutorial-to-read-data-from-text-files","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2017\/08\/vba-tutorial-to-read-data-from-text-files\/","title":{"rendered":"VBA Guide to Interact with Text Files \u2013 Part \u2013 2 of 2"},"content":{"rendered":"
[fusion_text]D<\/span>ear Friends,<\/p>\n I am back with the second part of the tutorial- VBA Guide to Interact with Text Files \u2013 Part \u2013 1 of 2<\/a><\/em>. This is the final part of this tutorial. Click on the below links to directly jump to that section…<\/p>\n <\/a><\/p>\n As I mentioned in my previous article, to read a Text File, we should open the existing Text File in Input Mode.<\/em><\/strong>. To know more about How to open Text Files in Excel VBA, read this article. <\/a><\/p>\n <\/i> This is very important to know that, <\/strong> Input statement is used to read a Text File which is produced or written by Write statement<\/em> while writing it via VBA code. As you know from the previous article, Write statement, writes the data in Text file in comma separated columns. Therefore, you will see that while reading the data from Text file, each comman separated data is stored in each variable specified in the below Syntax.<\/em><\/p>\n Let see the syntax of Input Statement which is used to read an existing Text File..<\/p>\n Input #FileNumber<\/em>, DataInput1, DataInput2, DataInput3<\/em>…<\/strong><\/p>\n When Text files are opened then windows recognize them by a unique integer value. Valid range of Integers for this parameter is between 1 to 511. Comma separated data which is stored in the Text file gets stored automatically in these DataInput variables provided. Let’s take an example and understand how Input statement works for reading data from a Text File.<\/p>\n This is the text file which we want to read the data from and put it in to Excel. using the below code, we will read this text file and store all data row by row in an excel sheet. (Lat’s say Input sheet)<\/p>\n <\/a> EOF() is a Boolean type function which checks if control has reached to the end of the Text File while reading lines in the text file.<\/p>\n EOF(FileNumber<\/em>)<\/strong> This function returns a Boolean – True\/False. If control reaches to an end then, it returns True <\/strong>when it reaches to the end of the file. Till then it always returns False<\/strong><\/p>\n In case you do not use EOF() to find the end of the text file – as soon as control reaches to the end of the text file, it tries to read something which does not exist and it gives a run time error – <\/p>\n [highlight color=”yellow” rounded=”” class=”” id=””]Run time Error – 62 – Input past end of file.[\/highlight]<\/p>\n After running the above code, this is what you get in your excel sheet Read Text File in Excel VBA<\/p><\/div><\/p>\n <\/a><\/p>\n As the name suggests, using this statement you can read the whole line at once and it gets stored in a single variable. Unlike Input statement, as explained above, you do not need to define multiple variable to store the data. Let see the syntax of Line Input Statement<\/em> which is used to read an existing Text File..<\/p>\n Line Input #FileNumber<\/em>, LineData<\/strong><\/p>\n When Text files are opened then windows recognize them by a unique integer value. Valid range of Integers for this parameter is between 1 to 511. Variable to store data of a line in Text file. It stores all data of a line.<\/p>\n Let’s take an example and understand how Line Input statement works for reading data from a Text File.<\/p>\n
\nIn previous article, mainly we learn about How to Open a Text File and How to write to Text File using VBA code. There were some other stuff as well which we discussed in detail. I would recommend to read my previous article before reading this article.<\/p>\nTopics covered in this Article<\/h1>\n
<\/i> Read data from Text File using VBA Code – Input <\/em><\/strong>Statement<\/a><\/h2>\n
<\/i> Read data from Text File using VBA Code – Line Input<\/strong><\/em> Statement<\/a><\/h2>\n
<\/i> What is EOF() function ? and What is its use here while interacting with text Files?<\/a><\/h2>\n<\/div>\n<\/div>\n
1. Read data from Text File using VBA Code – Input <\/em><\/strong>Statement<\/h1>\n
Syntax of Input Statement to Read data from Text File in Excel VBA<\/h2>\n
Where:<\/h2>\n
#FileNumber :<\/h2>\n
\nAs I mentioned above, it should be a unique integer, it is challenging for you to give a fixed number here in case you are dealing with multiple text files. To overcome this challenge you can use a function called FreeFile(). Click here to know more about FreeFile()<\/a> Function.<\/p>\nDataInput 1, 2, 3.. etc<\/h2>\n
\nNote:<\/strong> You can use one or more than one variables in this statement based how many comma separated data you want to read from the Text File. Therefore it is possible that there are 100s of columns are there in each rows in a TextFile but you can read only one or more specific data using Line statement.<\/p>\nExample:<\/h1>\n
\n<\/p>\n
\r\n\r\nSub ReadTextFileUsingInputStatement()\r\n\r\nDim sFilePath As String\r\nDim iRow As Integer\r\nDim OrderDate As Date\r\nDim OrderPriority As String\r\nDim OrderQuantity As Integer\r\nDim Discount As Double\r\nDim ShipMode As String\r\nDim CustomerName As String\r\nDim ShipDate As Date\r\n \r\n iRow = 2\r\n sFilePath = "C:\\Users\\vmishra\\Desktop\\LEM.txt"\r\n ' unique file number to access the file uniquely\r\n filenumber = FreeFile\r\n ' to check if file name LEM.txt exists\r\n ' if not, end the program\r\n If (VBA.Len(VBA.Dir(sFilePath))) = 0 Then MsgBox "File Does not exists": End\r\n ' Open the TextFile in Input mode\r\n ' in order to write in something\r\n Open sFilePath For Input As #filenumber\r\n \r\n ' Now using do while loop, traverse the Text file\r\n ' till it finds the End of the file.\r\n Do\r\n ' Now read the Text file data in each of the corresponding\r\n ' variables. As soon as below statement gets executed,\r\n ' corresponding data like OrderDate, ShipMode etc gets\r\n ' stored in the respective variables\r\n Input #filenumber, OrderDate, OrderPriority, OrderQuantity, Discount, ShipMode, ShipDate\r\n ' Now store these data in excel columns for each row.\r\n With Sheets("Input")\r\n .Cells(iRow, 1).Value = OrderDate\r\n .Cells(iRow, 2).Value = OrderPriority\r\n .Cells(iRow, 3).Value = OrderQuantity\r\n .Cells(iRow, 4).Value = Discount\r\n .Cells(iRow, 5).Value = ShipMode\r\n .Cells(iRow, 6).Value = CustomerName\r\n .Cells(iRow, 7).Value = ShipDate\r\n End With\r\n ' go to the next row in Excel sheet\r\n iRow = iRow + 1\r\n ' Go one by one till the last line of the file\r\n Loop Until EOF(filenumber)\r\n \r\n ' Close the file once all data\r\n ' is written in text file\r\n Close #filenumber\r\nEnd Sub\r\n<\/code><\/pre>\n
\nIn the above code you can see, I have used a function called EOF<\/strong>. Let’s have a quick look what this function is and why is it important to use?<\/p>\nWhat is EOF <\/em>Function<\/h1>\n
Syntax of EOF File<\/h2>\n
\nWhere FileNumber is same as explained above. It is an integer type variable. <\/p>\nWhy EOF() is important to use? <\/h2>\n
\n2. Read data from Text File using VBA Code – Line Input <\/em><\/strong>Statement<\/h1>\n
\nLine Input Statement should be, ideally, used to read the Text file which has different data which are separated by a delimiter. So that once you read the whole line by using Line Input statement and data is stored in a single variable then you can easily split data by using Split function<\/a>.<\/p>\nSyntax of Line Input Statement to Read data from Text File in Excel VBA<\/h2>\n
Where:<\/h2>\n
#FileNumber :<\/h2>\n
\nAs I mentioned above, it should be a unique integer, it is challenging for you to give a fixed number here in case you are dealing with multiple text files. To overcome this challenge you can use a function called FreeFile(). Click here to know more about FreeFile()<\/a> Function.<\/p>\nLineData<\/h2>\n
Example:<\/h1>\n