读取文本文件获取数据

此代码片段读取一个文本文件,并提取到工作表中的数据。

解释

这个简短的程序提取物在预定义文件夹或目录预定义的文本文件中的文本中。该程序可以通过许多修改文本文件来循环使用在目录“列出文件”代码,这需要自行修改。

整个VBA Excel程序可用于在本页面底部,下载欣赏!

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

 

 

 

下载Excel文件!Read_Text_File_Fetch_Data.xls

 

Add your comment

Your name:
Subject:
Comment: