2012-10-24 39 views
0

我在此網站上發現了此代碼,但我無法根據自己的需要對其進行調整,但我認爲它必須是非常快速解決。將文本文件導入到Excel中 - 每個文件的行都放在同一行的不同列中

該代碼將一系列文本文件導入到excel中。打開一個文件,該文件的第一行放在A1中,第二行放在A2中,依此類推。當打開一個新文件時,文本被放置在列A中的下一個可用單元格中(所有文件都讀入A列)。

我想稍作修改。我需要A1中文件1的第一行,B1中的第二行等等(即文件1中的所有行保存在第1行中)。然後,文件2中的行放在第2行,第3行中的文件3等。

任何幫助將不勝感激!

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:\YourDirectory\") 

    ' 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 

回答

0

Yeap。挺容易。只需調整列和行的偏移量,並且不會在讀取每行時對其進行分隔。

請參見下面的代碼調整:

Sub ReadFilesIntoActiveSheet() 

Dim fso As FileSystemObject 
Dim folder As folder, file As file, FileText As TextStream 
Dim TextLine As String, Items() As String 
Dim i As Long, cl As Range 

' Get a FileSystem object 
Set fso = New FileSystemObject 

' get the directory you want 
Set folder = fso.GetFolder("D:\YourDirectory\") 

Dim x As Long 
x = 1 'to offset rows for each file 

' Loop thru all files in the folder 
For Each file In folder.Files 

    ' set the starting point to write the data to 
    Set cl = ActiveSheet.Cells(x, 1) 

    ' Open the file 
    Set FileText = file.OpenAsTextStream(ForReading) 

    Dim i As Long 
    i = 0 'to offset columsn for each line 
    ' Read the file one line at a time 
    Do While Not FileText.AtEndOfStream 

     TextLine = FileText.ReadLine 'read line 

     cl.Offset(, i).Value = TextLine 'fill cell 

     i = i + 1 
    Loop 

    ' Clean up 
    FileText.Close 

    x = x + 1 

Next file 

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

End Sub 
+0

謝謝@ScottHoltzman,這正是我上面所要求的。但是,當我運行代碼時,我意識到我忘記了關於我的文本文件的一件重要事情。每個文件中經常有許多不同的「部分」。我希望每個部分都是我的excel文件中的一個單獨的行。在每個文件的開頭有3或4行介紹性文字,然後每一部分以含有單詞「FINEX」的行開始。如何修改代碼以在Excel中遇到與單詞FINEX一致的行時在Excel中啓動新行?提前致謝 – Brackers

0

我想認爲將所有對行和列的引用替換就足夠了。嘗試:

  1. 更換cl.Offset(0, i).Value = Items(i)cl.Offset(i, 0).Value = Items(i)

  2. 更換Set cl = cl.Offset(1, 0)Set cl = cl.Offset(0, 1)

這是否做的伎倆?

+0

謝謝,但並非精確做到這一點我很害怕,這一切都寫在第一行。它比我更接近。我在(1)中做了修改,但沒有修改'set cl'行 – Brackers

相關問題