2016-09-13 115 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

相关问题