This code snippet reads a text file and fetches the data into the worksheet.
This 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!
Public 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
Function ReadTextFileFetchDataMain(ReadTextFile As String) As String
Dim ReadTextFileSource As Integer
Dim ReadTextFile2 As String
'Closes text files that might be opened
'The number of the next free text file
ReadTextFileSource = FreeFile
Open ReadTextFile For Input As #ReadTextFileSource
ReadTextFile2 = Input$(LOF(1), 1)
ReadTextFileFetchDataMain = ReadTextFile2
Download excel file! Read_Text_File_Fetch_Data.xls