Read Text File Fetch Data

Read Text File Fetch Data

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

End Sub

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

End Function




Download excel file! Read_Text_File_Fetch_Data.xls


This website is very very helpful and the VB codes are just perfect...

Comment made by: Amaan , 2009-03-27 14:01:28

Hi Amaan! I thank you for your feedback. I have collected the VBA macro code I have developed during my VBA-life ;)

Comment made by: Johan (Administrator) , 2009-03-27 16:42:27


Comment made by: arobreizh29 , 2009-09-01 23:27:57

my query is how to break with VBA Code in excel address format Example 1 Shop No 1,Ashoka Building, Near KFC Restaurant, Janakpuri, Delhi So how to break the address in excel column My Excel Format This Example 2 according to 1 example Shop No Column ! Building Name Column ! Near Name Column ! Area Column! Please Send Me my email ID,

Comment made by: Sarthak Thakur , 2013-04-26 06:39:10

muyu bueno, gracias por los códigos

Comment made by: Ramiro Espejo , 2015-07-06 15:31:52

Write Comment:


two + 3 minus 1=

Your name: