我有一個帶有TXT文件(一列)的文件夾。我需要將它們導入到一個Worksheet中,但每個文件都應該放在新列中。將文件名稱添加爲標題會很好。將多個TXT/CSV導入一個Excel工作表,但下一列中的每個文件
我試圖在新文件打開之前增加cl
元素。是好方向嗎?
Set cl = ActiveSheet.Cells(1, i)
i = i + 1
如何修改以下代碼?任何幫助將不勝感激!
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:\Projects\Data\")
' 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
您發佈似乎沒有一個良好的匹配問題的代碼:這將有助於從後輸入文件的一個樣本的一些日期,也顯示應該如何看在工作表上。 –
我不確定這裏共享示例數據的最佳方式是什麼。讓我們試試這個圖像:[鏈接] http://i66.tinypic.com/a3jdde.png A列包含第一個文件中的示例。列C-E與其他文件結合後期待看到的內容。文件名是可選的。數據應該從A列開始。謝謝! – Adam