2017-09-18 2 views
1

매일 보고서를 얻습니다.이 보고서에는 약 8500 개의 행이 있습니다. 수동으로 수행 한 작업을 수행하는 매크로를 만들려고했습니다. 보고서의 문제는 모든 행이 동일한 형식이 아니라는 것입니다 (즉, 행 1 : 숫자, 텍스트, 텍스트, 숫자 및 행 2 : 텍스트, 숫자, 숫자, 텍스트).두 파일을 비교하여 다른 행을 출력합니다.

새 파일을 이전 파일과 비교하여 새로운 차이점을 출력하고 싶습니다. 나는 2 개의 파일을 통해 실행하는 매크로를 얻을 수 있지만 그것은 다른 라인으로 플래그를 지정하지 않습니다.

어제 파일 :

10001,April,Apple 
10002,Book,Bush 
10004,Dog,Days 
10006,Free,Food 
10008,Happy,Help 
10009,Ikky,Icing 
10010,Jamming,Jupiter 

오늘 파일 :

10001,April,Apple 
10002,Book,Bush 
10003,Cat,Cattle 
10004,Dog,Days 
10005,Echo,Eggg 
10006,Free,Food 
10007,Good,Game 
10008,Happy,Help 
10009,Ikky,Icing 
10010,Jamming,Jupiter 

참고 :

Sub test() 

Dim yesterdayFile As String 
Dim todayFile As String 

yesterdayFile = Application.GetOpenFilename() 
todayFile = Application.GetOpenFilename() 
Dim yesterdayLine As String 
Dim todayLine As String 
Dim txt As String 
Dim i, j, k, sameLine As Integer 
Dim wkbTemp As Workbook 
i = 1 
j = 1 
k = 1 
sameLine = 0 


Open yesterdayFile For Input As #1 
Do Until EOF(1) 
    sameLine = 1 'reset write operator 
    Open todayFile For Input As #2 
Line Input #1, yesterdayLine 
    Do Until EOF(2) 
     Line Input #2, todayLine 
     If StrComp(yesterdayLine, todayLine) = 0 Then 'compare lines in files if same then flag write operator to 1 
      sameLine = 1 
     End If 
     j = j + 1 'inner loop counter 
    Loop 
If sameLine = 0 Then 'if write operator is not active then output line 
    Cells(i, 1) = yesterdayLine 
    i = i + 1 'counter for cells 
End If 
Close #2 
k = k + 1 'outer loop counter 
Loop 

'test line to see if its eof 
Cells(1, 10) = i 
Cells(2, 10) = j 
Cells(3, 10) = k 

Close #1 



End Sub 

테스트 파일을 빠르게 테스트를 실행하기 위해 사용중인 실제 데이터에서 더이 '고유 ID 필드'

카운터가 올바른 숫자로 끝나기 때문에 재귀를 통해 실행되는 것으로 알고 있습니다.

EDIT : 다른 언어에서도 쉽게 수행 할 수 있지만 업무용 터미널에서 엑셀 vba에만 액세스 할 수 있으며 네트워크에서 파일을 가져올 수 없습니다. .

+0

정확한 세포는 달라야합니까? 또는 한 줄의 순서가 다르지만 그 값이 같으면 괜찮습니까? – danieltakeshi

+0

직접 셀이 아닌 파일에서 입력 행을 테스트하고 있습니다. CSV 파일이므로 전체 행이 "텍스트"로 나열됩니다. "숫자"| "숫자"| "본문". 구분은 실제로 출력 할 줄을 결정할 때 문제가됩니다. – Nealin

+0

[array] (https://stackoverflow.com/questions/34563525/improving-the-performance-of-for-loop/34564306#34564306)로 작업하고 다차원 배열을 모두 일치시켜야한다고 생각합니다. 또는 셀에 csv 데이터를 입력하고 스프레드 시트에서 일치시킬 수 있습니다. [방법의 성능] (https://fastexcel.wordpress.com/2011/10/26/match-vs-find-vs-variant-array-vba-performance-shootout/) – danieltakeshi

답변

0

그래서 많은 시행 착오 끝에 나는 내 자신의 질문에 답했습니다. 모든 답변을 주셔서 감사합니다. 그러나 72 백만 회 반복에서 3bil로 전환하는 것은 선택 사항이 아닙니다.내 코드는이 .txt 인 및 .CSV에서 작동하고 세포를 쓰기 전에 직접 입력 라인을 비교

Sub test() 
'Freeze window 
Application.ScreenUpdating = False 
Application.Calculation = xlCalculationManual 

'open files to edit 
Dim fileA, fileB As String 
fileA= Application.GetOpenFilename() 
fileB = Application.GetOpenFilename() 

'setting variables 
Dim lineA, lineB, DQ As String 'read in lines and double quote  variables 
Dim i, sameLine As Integer 'row counter and testing(could have used boolean?) 
Dim newLine 'object creation for array of line 
i = 1 
DQ = Chr(34) 'character 34 is " 

Open fileA For Input As #1 'open file 1 for append 
Do Until EOF(1) 'Outter loop to run through file 1 
    sameLine = 0 'reset write operator 
    Open fileB For Input As #2 'open file 2 for append 
    Line Input #1, lineA'read in line from file 1 
    Do Until EOF(2) 'inner loop to run through file 2 
     Line Input #2, lineB 'read in line from file 2 
     If StrComp(lineA, lineB) = 0 Then 'compare lines in files if same then flag write operator to 1 
      sameLine = 1 
     End If 
    Loop 
    If sameLine = 0 Then 'if write operator is not active then output line 
     count = Len(lineA) - Len(Replace(lineA, "|", ""))  'count number of columns needed for output 
     lineA= Replace(lineA, DQ, "") 'removing all double  quotes from line 
     newLine = Split(lineA, "|") 'spliting line into object with | as delimiter 
     For counter = 1 To count 'placing line in row 
      Cells(i, counter) = newLine(counter - 1) 
     Next counter 
     i = i + 1 'counter for cells 
    End If 
Close #2 
Loop 
Close #1 
'unfreezing window 
Application.Calculation = xlCalculationAutomatic 
Application.ScreenUpdating = True 
End Sub 

처럼 보이는 결국 무엇

. 내 문제는 수정하기 위해 몇 줄을 추가 한 각 줄 끝에 타임 스탬프가 있기 때문에 독특했습니다.

0

만약 내가 이해하고 있다면, 각각의 wb에서 같은 수의 라인/작업을 가정 할 때 이전 및 새로운 통합 문서에서 사용되는 4 개의 필드가 있습니다. 그것은 꽤 아니지만, 당신은 유사한 평가할 수 : 최신 통합 문서를 사용하여 표시하기 위해 wbNew 및 shNew를 사용

Dim i as Long, j as Long, k as Long, l as Long, m as Long 

If wbNew.shNew.Cells(i, 1).Value = wbOld.shOld.CellS(i,1).Value OR wbNew.shNew.Cells(i, 1).Value = wbOld.shOld.CellS(i,2).Value OR wbNew.shNew.Cells(i, 1).Value = wbOld.shOld.CellS(i,3).Value OR wbNew.shNew.Cells(i, 1).Value = wbOld.shOld.CellS(i,4).Value OR Then 
    j=1 
End If 

If wbNew.shNew.Cells(i, 2).Value = wbOld.shOld.CellS(i,1).Value OR wbNew.shNew.Cells(i, 2).Value = wbOld.shOld.CellS(i,2).Value OR wbNew.shNew.Cells(i, 2).Value = wbOld.shOld.CellS(i,3).Value OR wbNew.shNew.Cells(i, 2).Value = wbOld.shOld.CellS(i,4).Value OR Then 
    k=1 
End If 

If wbNew.shNew.Cells(i, 3).Value = wbOld.shOld.CellS(i,1).Value OR wbNew.shNew.Cells(i, 3).Value = wbOld.shOld.CellS(i,2).Value OR wbNew.shNew.Cells(i, 3).Value = wbOld.shOld.CellS(i,3).Value OR wbNew.shNew.Cells(i, 3).Value = wbOld.shOld.CellS(i,4).Value OR Then 
    l=1 
End If 

If wbNew.shNew.Cells(i, 4).Value = wbOld.shOld.CellS(i,1).Value OR wbNew.shNew.Cells(i, 4).Value = wbOld.shOld.CellS(i,2).Value OR wbNew.shNew.Cells(i, 4).Value = wbOld.shOld.CellS(i,3).Value OR wbNew.shNew.Cells(i, 4).Value = wbOld.shOld.CellS(i,4).Value Then 
    m=1 
End If 

If (i+j+k+l)=4 Then 
    wbNew.shNew.Rows(i).Interior.Color=2 
End If 

j=0 
k=0 
l=0 
m=0 

및 wbOld 및 놈이야 어제의 통합 문서에 대한. 이것은 모두 루프 안에 있고, 마지막 행을 찾아야합니다. 당신은 줄에 고유 한 어떤이 있다면 당신이 할 수 있도록,

z = Application.Match(wbNew.shNew.Cells(i,1),wbOld.sheOld.Columns(1)).Row 
If wbNew.shNew.Cells(i,1).Value = wbOld.shOld.Cells(z, 1).Value OR wbNew.shNew.Cells(i,1).Value = wbOld.shOld.Cells(z, 2).Value OR wbNew.shNew.Cells(i,1).Value = wbOld.shOld.Cells(z, 3).Value OR wbNew.shNew.Cells(i,1).Value = wbOld.shOld.Cells(z, 4).Value Then 
    j=1 
End If 

이 더 많은 경우를 다음과 같습니다


당신은 또한 (찾기 사용하는 접근 방식을 취할 수) 또는 매치(), 있도록 z를 찾은 다음 하나의 통합 문서/시트를 반복 할 필요가있는 비교 만 수행하십시오.


편집 :

Dim r as long, c as Long 

For r = 1 to LR 
    For c = 1 to LC 
     If Cells(r, c).Value = "Moo" Then 
      If Cells(r, c).Interior.Color <> 2 Then 
       Cells(r, c).Interior.Color=2 
      End If 
     End If 
    Next c 
Next r 

그것은 도움이 될 수 있습니다 :

행과 열 (중첩 루프) 두 통해 반복, 진정한 플래그 셀 내부의 경우 마킹의 예를 추가 차원과 함께 작업하려는 셀을 만들려면 다음과 같이하십시오.

Dim y as Variant 

y = wbNew.shNew.Cells(r, c).Value 

이렇게하면 편집하기가 더 쉽습니다.

+0

예제 코드는 몇 가지 필드에 불과합니다. 실제 데이터 범위는 19에서 42 필드입니다. 그래서 이것을 42 개 필드에 사용하는 것은 상당히 많은 일이 될 것입니다. 그래서 나는 입력 라인을 서로 직접 테스트하고 있습니다. – Nealin

+0

@Nealin 페이지에 헤더가 전혀 없습니까? 당신이 다루는 정확한 세포를 나타낼 수 있도록 쉽게 만들 것입니다. – Cyril

+0

아쉽게도 파일은 약 8 개의 개별 테이블을 포함하는 CSV 파일이므로 헤더가 파일의 전체 길이에 대해 작동하지 않습니다. – Nealin

관련 문제