Read Text File Fetch Data

This code snippet reads a text file and fetches the data into the worksheet.

Explanation

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!
 

 

Code

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

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

 
Comments (3)
so thx because I'm WINNewbie
3 Tuesday, 01 September 2009 23:27
arobreizh29
................thx
Feedback
2 Friday, 27 March 2009 16:42
Johan (Administrator)
Hi Amaan!

I thank you for your feedback. I have collected the VBA macro code I have developed during my VBA-life ;)
Survey
1 Friday, 27 March 2009 14:01
Amaan
This website is very very helpful and the VB codes are just perfect...

Add your comment

Your name:
Subject:
Comment: