일반적으로 파일을 처리하는 좋은 방법은 'FileSystemObject'입니다. VBA에서이 사용할 수 있도록하려면 당신은에 대한 참조를 추가해야합니다
이
(도구 \ 참조 메뉴를 선택 참조 대화에서 '마이크로 소프트 런타임 스크립팅'를 선택합니다.)
다음 코드 예제는 모두 읽습니다 파일을 폴더에 저장하고 한 번에 한 줄 씩 내용을 읽고 각 줄을 | 셀 A1에서 시작하여 라인 당 한 행씩 활성 시트에이 비트를 씁니다.
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
하위 의도적으로 (내가) 희망 선명하게 유지하기 위해 간단하고 강력하게 할 일을해야합니다 (예 : 오류 처리 추가)
이 먼저 가입하고 결과를 가져 오기? 탭으로 구분하려는 사용자의 경우 –