2014-10-17 3 views
1

간단한 데이터 구문 분석을 시도 할 때마다 프로그램이 충돌하기 때문에 Excel VBA에서 대용량 데이터 파일을 구문 분석하는 방법을 알고 있는지 궁금합니다. 데이터는Excel에서 데이터 구문 분석으로 인해 충돌이 발생한다

593972,Data,15:59:59.820,9519,9519,Px(25.5),9519,9500,10001,10226,10451,0,0,0,0,0,28.7604,25.4800,25.4841 

으로 포맷과 완전히 동일한 포맷 약 3,000,000 라인이 있고 (이 593,972 인 위의 경우에) 상기 제 값이 경우 I 라인에 특정 값을 빼 할 특정 번호. 나는 VBA에 새로운 것이므로 어떤 도움을 주시면 감사하겠습니다. 시간 내 주셔서 감사합니다.

+0

csv로 작성된 외부 프로그램에서 생성 된 내용을 파싱하고 있습니까? Excel에서 – hedgedandlevered

+2

"300 만 줄"? – pnuts

+0

외부 파일은 .txt 파일이고 예 .txt 파일에는 약 3 백만 행이 있지만 특정 선행 번호가있는 파일 만 가져 오려고합니다. –

답변

1

FSO를 사용해보십시오. 귀하의 필요에 맞게 수정하십시오. 아래 Sub

Sub ParseFile() 

Dim fso As Object 
Set fso = CreateObject("Scripting.FileSystemObject") 

Dim strLine As String 
Dim arrLine() As String 
Dim objFile 
Const ForReading = 1, ForWriting = 2, ForAppending = 8 

Set objFile = fso.OpenTextFile("C:\Temp\Text File.txt", ForReading) '<modify path as needed 

Do Until objFile.AtEndOfStream 
    strLine = Trim(objFile.Readline) 
    If (strLine <> "") Then 
     arrLine = Split(strLine, ",") 'one dimensional array 

     'parse the arrLine to test for the data you need 
     Dim FirstValue as String 
     FirstValue = arrLine(0) 

     If FirstValue = "593972" Then 

      'put the data in Excel if desired/needed 

     End If 

    End If 
Loop 

objFile.Close 
Set objFile = Nothing 

End Sub 
0

줄 단위로 판독하고, 텍스트 스트림이 열리고, 제 1 필드가 각각의 라인에 대한 소정의 값을 갖는 경우를 검증; 당신이 원하는대로 할 수 있도록 적응 시키십시오 :

Public Sub ReadAndValidate(_ 
    ByVal FileName As String, _ 
    ByVal FieldKey As String _ 
) 
     ' This function doesn't do error handling, assumes that the ' 
     ' field separator is "," and that the key field is first. ' 
     ' It uses the "Scripting" lib; "Microsoft Scripting Runtime"' 
     ' needs to be referenced by the containing workbook.  ' 

     Dim line As String 
     Dim keylen As Long 

     Dim fs  As Scripting.FileSystemObject 
     Dim f  As Scripting.TextStream 

     Let FieldKey = FieldKey & "," ' add the separator to the key ' 
     Let keylen = Strings.Len(FieldKey) 

     Set fs = CreateObject("Scripting.FileSystemObject") 
     Set f = fs.OpenTextFile(_ 
      FileName:=FileName, _ 
      IOMode:=IOMode.ForReading _ 
     ) 

     While Not f.AtEndOfStream 
       Let line = f.ReadLine() 
       If Strings.Left$(line, keylen) = FieldKey Then 
         ' replace the statement below with your code ' 
         Debug.Print line 
       End If 
     Wend 

     f.Close 

End Sub 
관련 문제