2016-09-13 46 views
0

我有一個帶有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 
+1

您發佈似乎沒有一個良好的匹配問題的代碼:這將有助於從後輸入文件的一個樣本的一些日期,也顯示應該如何看在工作表上。 –

+0

我不確定這裏共享示例數據的最佳方式是什麼。讓我們試試這個圖像:[鏈接] http://i66.tinypic.com/a3jdde.png A列包含第一個文件中的示例。列C-E與其他文件結合後期待看到的內容。文件名是可選的。數據應該從A列開始。謝謝! – Adam

回答

0

事情是這樣的:

Sub ReadFilesIntoActiveSheet() 

    Dim fso As FileSystemObject 
    Dim folder As folder 
    Dim file As file 
    Dim FileText As TextStream 
    Dim i As Long 
    Dim cl As Range 

    Set fso = New FileSystemObject 
    Set folder = fso.GetFolder("d:\Projects\Data\") 

    Set cl = ActiveSheet.Cells(1, 1) 

    Application.ScreenUpdating = False 

    For Each file In folder.Files 

     Set FileText = file.OpenAsTextStream(ForReading) 
     cl.Value = file.Name 
     i = 1 

     Do While Not FileText.AtEndOfStream 
      cl.Offset(i, 0).Value = FileText.ReadLine 
      i = i + 1 
     Loop 

     FileText.Close 

     Set cl = cl.Offset(0, 1) 
    Next file 

    Application.ScreenUpdating = True 

    Set FileText = Nothing 
    Set file = Nothing 
    Set folder = Nothing 
    Set fso = Nothing 

End Sub 
+0

正是我需要的。謝謝蒂姆! – Adam

0

我假定你的文本文件數據都適合整齊地放在一列中。如果你正在迭代這樣的行和列,我總是傾向於使用Cells(x, y).Value語法。

不能保證它是最有效的方法,但這似乎是一個足夠簡單的過程,性能無關緊要。

你可以調整你的代碼做這樣的事情:

Dim RowIndex As Long 'Excel Rows need the Long data type 
    Dim ColumnIndex As Integer ' Not as many columns, so use Integer 

    ' Start at column 1. 
    ColumnIndex = 1 

    ' Do the rest of your file I/O and split into the Items array. 

    ' Here's your column header: 
    Cells(1, ColumnIndex).Value = file.Name 

    ' Start the actual data in row 2. 
    RowIndex = 2 
    For (i = 0 to UBound(Items)) 
     Cells(RowIndex, ColumnIndex).Value = Items(i) 
     RowIndex = RowIndex + 1 
    Next i 

    ' When you're all done, advance ColumnIndex so the next time through 
    ' the loop you're outputting on the next Column. 
    ColumnIndex = ColumnIndex + 1 

這應該是功能上等同於你正在使用的語法Offset,但我發現,使用Cells使得它更加明顯發生了什麼指數(行或列或兩者)你迭代。

+0

謝謝你的代碼。我會盡量使用! – Adam

相關問題