我有大約600個文本文件。每個文件包含2列,並且是space delimited
。有什麼辦法可以將它們全部導入到同一個Excel電子表格中?將多個文本文件導入Excel
我看到一篇關於這個帖子的文章,並使用了下面的腳本,但那對我沒用。這紅粉我User-defined type not defined
Sub ReadFilesIntoActiveSheet()
Dim fso As FileSystemObject
Dim folder As folder
Dim file As file
Dim FileText As TextStream
Dim TextLine As String
Dim Items() As String
Dim i As Long
Dim cl As Range
' Get a FileSystem object
Set fso = New FileSystemObject
' get the directory you want
Set folder = fso.GetFolder("D:\mypath\")
' set the starting point to write the data to
Set cl = ActiveSheet.Cells(1, 1)
' Loop thru all files in the folder
For Each file In folder.Files
' Open the file
Set FileText = file.OpenAsTextStream(ForReading)
' Read the file one line at a time
Do While Not FileText.AtEndOfStream
TextLine = FileText.ReadLine
' Parse the line into | delimited pieces
Items = Split(TextLine, "|")
' Put data on one row in active sheet
For i = 0 To UBound(Items)
cl.Offset(0, i).Value = Items(i)
Next
' Move to next row
Set cl = cl.Offset(1, 0)
Loop
' Clean up
FileText.Close
Next file
Set FileText = Nothing
Set file = Nothing
Set folder = Nothing
Set fso = Nothing
End Sub
`
感謝您的幫助!
可以將'Windo ws腳本運行時'參考下面提到的,或更改這2行:'Dim fso As FileSystemObject'和'Dim FileText As TextStream' to'... As Object',然後'Set fso = New FileSystemObject'設置爲'Set fso =的CreateObject( 「Scripting.FileSystemObject的」)'。同時進行下面@mkingston提到的更改。 – transistor1 2012-03-23 02:54:06