2013-03-08 61 views
0

我一直在絞盡腦汁試圖在Excel中創建的宏將打開所有.txt文件中的特定文件夾,並將它們導入到接下來的導入文件夾可用的行。數據是製表符分隔的,第一個文件需要導入到單元格B8,下一個文件B9,下一個B10等。Excel VBA中 - .TXT(製表delimted)文件到下一個可用行

我用這個代碼約80%,但它將所有數據導入到一個單元格(B8),而不是製表符分隔成行(B8,C8,D8,E8等)。

Sub Read_Text_Files() 
Dim sPath As String, sLine As String 
Dim oPath As Object, oFile As Object, oFSO As Object 
Dim r As Long 
' 
'Files location 
sPath = "C:\Test\" 

'Text to Columns 
Range("A1", Range("A" & Cells.Rows.Count).End(xlUp)).Select 
Selection.TextToColumns DataType:=TabDelimited, TextQualifier:= _ 
     xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _ 
     Comma:=False, Space:=False, Other:=False 
Application.ScreenUpdating = True 

r = 8 
Set oFSO = CreateObject(_ 
"Scripting.FileSystemObject") 
Set oPath = oFSO.GetFolder(sPath) 
Application.ScreenUpdating = False 
For Each oFile In oPath.Files 

If LCase(Right(oFile.Name, 4)) = ".txt" Then 

Open oFile For Input As #1 

Do While Not EOF(1) ' Loop until end of file. 
Input #1, sLine ' Read data 
Range("B" & r).Formula = sLine ' Write data line 

r = r + 1 
Loop 
Close #1 ' Close file. 
' 
End If 
Next oFile 
End Sub 
+0

PS:我有VBA,它允許數據被導入到電子表格和粘貼數據成單個細胞(製表符分隔),但我不能工作了如何配合兩個在一起.. ' '打開文本文件 Workbooks.OpenText Filename:= strInFile,Origin:= 65001,StartRow:= 1,DataType:= xlDelimited,_ TextQualifier:= xlDoubleQuote,ConsecutiveDelimiter:= False,Tab:= True,FieldInfo:= Array (1,1),_ TrailingMinusNumbers:= TRUE; – 2013-03-08 16:56:54

+0

你說的第一個文件進入B8,下入B9,等等......是每個文件只有一條線路長? – 2013-03-08 18:33:22

+0

您可以使用'Split(sLine,vbTab)'來創建一個值的數組,然後您可以將其分配給工作表。 – 2013-03-08 19:21:46

回答

0

我建議繼續爲您在您的評論中提到,使用Workbooks.OpenText打開每個文件,然後每行從打開的工作簿複製到指定的頁面。

Sub Read_Text_Files() 
    Dim sPath As String 
    Dim oPath, oFile, oFSO As Object 
    Dim r, iRow As Long 
    Dim wbImportFile As Workbook 
    Dim wsDestination As Worksheet 

    'Files location 
    sPath = "C:\Test\" 
    Set wsDestination = ThisWorkbook.Sheets("Sheet1") 

    r = 8 
    Set oFSO = CreateObject("Scripting.FileSystemObject") 
    Set oPath = oFSO.GetFolder(sPath) 
    Application.ScreenUpdating = False 
    For Each oFile In oPath.Files 
     If LCase(Right(oFile.Name, 4)) = ".txt" Then 
      'open file to impor 
      Workbooks.OpenText Filename:=oFile.Path, Origin:=65001, StartRow:=1, DataType:=xlDelimited, _ 
      TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, FieldInfo:=Array(1, 1), _ 
      TrailingMinusNumbers:=True 
      Set wbImportFile = ActiveWorkbook 
      For iRow = 1 To wbImportFile.Sheets(1).UsedRange.Rows.Count 
       wbImportFile.Sheets(1).Rows(iRow).Copy wsDestination.Rows(r) 
       r = r + 1 
      Next iRow 
      wbImportFile.Close False 
      Set wbImportFile = Nothing 
     End If 
    Next oFile 
End Sub 
+0

嘿羅斯,非常感謝您的回覆 - 它確實正是我現在需要的:)您是否知道如何將此粘貼到列B而不是列A?嘗試了幾件事情,但不斷得到調試錯誤!當然 – 2013-03-12 12:10:58

+0

@MicBurns,只是改變了線'wbImportFile.Sheets(1).Rows(iRow).Copy wsDestination.Rows(R)''到wbImportFile.Sheets(1).UsedRange.Rows(iRow).Copy wsDestination.Cells (r,2)'這將代替複製整行,僅複製單元格的UsedRange並插入目標工作簿的第2列。 – 2013-03-12 15:05:48

+0

嘿羅斯 - 完美,完全符合我的需求。不能謝謝你的幫助! :) – 2013-03-13 13:04:03

相關問題