Read Text File Fetch DataThis code snippet reads a text file and fetches the data into the worksheet.
ExplanationThis short program extracts the text stored in a predefined text file in predefined folder or directory. The program can be modify to loop through many text files by using the "List files in directory" code, this requires modification by yourself. When making programs that store data in text files not real databases this comes in handy. If you do not perform many database calls per seconds it is ok to use text files as database. The entire VBA Excel program is available for downloading at the bottom of this page, enjoy!
CodePublic Sub ReadTextFileFetchData()
Dim NameOfFile As String Dim PlaceOfFile As String Dim Filelocation As String
NameOfFile = Range("c6").Value PlaceOfFile = Range("c5").Value Filelocation = PlaceOfFile + "\" + NameOfFile sText = ReadTextFileFetchDataMain(Filelocation) Range("c10").Value = sText
End Sub
Function ReadTextFileFetchDataMain(ReadTextFile As String) As String Dim ReadTextFileSource As Integer Dim ReadTextFile2 As String
'Closes text files that might be opened Close 'The number of the next free text file ReadTextFileSource = FreeFile Open ReadTextFile For Input As #ReadTextFileSource ReadTextFile2 = Input$(LOF(1), 1) Close ReadTextFileFetchDataMain = ReadTextFile2 End Function
Download excel file! Read_Text_File_Fetch_Data.xls |
I thank you for your feedback. I have collected the VBA macro code I have developed during my VBA-life ;)