약 600 개의 텍스트 파일이 있습니다. 각 파일에는 두 개의 열이 있으며 space delimited
입니다. 같은 Excel 스프레드 시트로 모든 항목을 가져올 수있는 방법이 있습니까?여러 텍스트 파일을 Excel로 가져 오기
나는 이것에 관한 게시물을 보았고 다음 스크립트를 사용했지만 저에게는 효과가 없었습니다. 그것은 나에게 User-defined type not defined
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:\mypath\")
' 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
도움을
`
감사 gived!
ws Scripting Runtime' 레퍼런스를 수정하거나이 두 줄을 변경하십시오 :'Dim fso As FileSystemObject' 그리고'Dim FileText As TextStream'을'... As Object'로 변경 한 다음'Set fso = New FileSystemObject'를'Set fso = CreateObject ("Scripting.FileSystemObject")'입니다. 또한 아래 @mkingston에서 언급 한 변경 사항을 적용하십시오. – transistor1