[英]Extracting data from large number of text files to excel
I came across the VBA code (linked below) that I found very useful in importing data from text files into separate cells by defining a separator. 我遇到了VBA代码(在下面链接),发现通过定义分隔符将文本文件中的数据导入到单独的单元格中非常有用。 The current code allows a single file to be specified and the data extracted from it. 当前代码允许指定一个文件并从中提取数据。 What I'm hoping to achieve is to extract data from a number of text files and adding the data from each file into a new row in excel. 我希望实现的是从多个文本文件中提取数据,并将每个文件中的数据添加到excel中的新行中。 I'm having difficulties trying to add a loop into the code to achieve this. 我在尝试向代码中添加循环以实现此目标时遇到了困难。
Would you be able to advise how this could be achieved? 您能否建议如何实现?
http://www.cpearson.com/excel/ImpText.aspx http://www.cpearson.com/excel/ImpText.aspx
Sub ImportTextFile()
Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
FName = "C:\Users\40044600\Documents\zdump\"
MyFile = Dir(FName & "*.txt")
Sep = vbLf
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Do While MyFile <> ""
Open (FName & MyFile) For Input As #1
While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend
Close #1
MyFile = Dir()
Debug.Print text
Loop
EndMacro: On Error GoTo 0 Application.ScreenUpdating = True Close #1 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' END ImportTextFile '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' End Sub EndMacro:出现错误GoTo 0 Application.ScreenUpdating = True Close#1'''''''''''''''''''''''''''''''' ``'''''''''''''''''''''''''END ImportTextFile''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''结束子
Many thanks 非常感谢
Setup: In a test.txt files: 设置:在test.txt文件中:
Other text files with the same layout were created in the same directory. 具有相同布局的其他文本文件是在同一目录中创建的。
In a spreadsheet note the cell column headings and active cell location. 在电子表格中, 记下单元格列标题和活动单元格位置。
The single file code is wrapped in code that reads multiple files then calls the single file code. 单个文件代码包装在读取多个文件然后调用单个文件代码的代码中。 In this example uses all the text files. 在本示例中,将使用所有文本文件。 ( test*.txt ) with test for the start of their name. ( test * .txt ),并加上test作为其名称的开头。
Sub TxtFiles()
Dim strFileName As String
Dim strFolder As String
Dim strFileSpec As String
'TODO: Specify path spec
strFolder = "C:\Users\007\Documents\Programming\VBA\Excel"
'TODO: Specify file spec
strFileSpec = strFolder & "\test*.txt"
strFileName = Dir(strFileSpec)
Do While Len(strFileName) > 0
Call ImportTextFile(strFileName, "|")
'move active cell location to next available empty cell row in column A.
Range("A1").End(xlDown).Offset(1, 0).Select
'Read next filename
strFileName = Dir
Loop
End Sub
The above code calls the following code from ImportTextFile : 上面的代码从ImportTextFile调用以下代码:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ImportTextFile
' This imports a text file into Excel.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ImportTextFile(FName As String, Sep As String)
Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Application.ScreenUpdating = False
On Error GoTo EndMacro:
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Open FName For Input Access Read As #1
While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend
EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END ImportTextFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub
The end result will look like: 最终结果将如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.