2013-05-11 139 views
0

我在這裏得到了嚴重的問題..任何形式的幫助都非常感謝!比較使用vba的巨大文本文件

我有兩個巨大的文本文件(130 MB),每個文件都有數千個記錄。我需要使用vba或任何方式比較這兩個文件,並生成一個包含標題和兩個附加列的電子表格。這兩個附加列將是文件名,在下一列中它應顯示哪個特定列是錯誤的。每個記錄都會有多個差異。一個文件可以具有在其他文件中找不到的記錄。所以這個條件也應該記錄在電子表格中。

實施例:

Media Events: Taking one record from each. 
00000018063|112295|000|**0009**| 

PROL: 
00000018063|112295|000|**0013**| 

在上面的例子中,記錄是從兩個文件。突出顯示的是記錄之間的差異。所以輸出應該是這樣..

HH_NUMBER  | CLASS_DATE | MV_MIN DURATION File Mismatc  Mismatch Reason 
00000018063 | 112295 | 000 **0009** Media Events  Mismatches in DURATION 
00000018063 | 112295 | 000 **0013** PROL    Mismatches in DURATION 
00000011861 | 112295 | 002  0126  Media Events  missing in PROL file 
+0

爲了更好的可讀性,我可以建議您簡化例子,也許四列? – Floris 2013-05-11 18:16:22

+0

你是在問什麼?因爲現在看起來您必須理清流程圖以瞭解您可以解決問題的位置和流程。選擇一種語言和開始編碼階段是不是現在的第一件事... – 2013-05-11 18:20:59

+1

要得到一個有意義的答案,請閱讀常見問題的說明http://stackoverflow.com/questions/how-to-ask和我個人的最愛: http://mattgemmell.com/2008/12/08/what-have-you-tried – 2013-05-11 18:21:53

回答

2

似乎有三個問題在這裏:

1)找到兩個文件之間的匹配記錄(第一列)。

2)比較匹配的第一列的記錄 - 如果是有區別的,記錄有什麼不同的是

3)如果在一個文件中存在記錄,但不是別的,錄製。

我打算假設這兩個「巨大的文件」實際上是在同一個Excel工作簿中的單獨工作表,並且記錄按第一個鍵排序。這將顯着加快處理速度。但我認爲,速度是次要的問題。我還假設你有第三張表格放置輸出。

下面是VBA代碼的大綱 - 你需要做一些工作才能讓它「恰到好處」適用於你的應用程序,但是我希望這能夠讓你繼續。

Sub compare() 
Dim s1 as Worksheet 
Dim s2 as Worksheet 
Dim col1 as Range 
Dim col2 as Range 
Dim c as Range 
Dim record1 As Range, record2 As Range, output As Range 
Dim m 
Dim numCols as Integer 

numCols = 5 ' however many columns you want to compare over 

Set s1 = Sheets("Media") 
Set s2 = Sheets("Pro") 
Set output = Sheets("output").Range("A2") 

Application.ScreenUpdating = False 
s1.Select 
Set col1 = Range("A2", [A2].End(xlDown)); 
s2.Select 
Set col2 = Range("A2", [A2].End(xlDown)); 

On Error Resume Next 
For Each c in col1.Cells 
    m = Application.Match(c.Value, col2, 0); 
    If isError(m) Then 
    ' you found a record in 1 but not 2 
    ' record this in your output sheet 
    output.Value = "Record " & c.Value & " does not exist in Pro" 
    Set output = output.Offset(1,0) ' next time you write output it will be in the next line 
    ' you will have to do the same thing in the other direction - test all values 
    ' in 2 against 1 to see if any records exist in 2 that don't exist in 1 
    Else 
    ' you found matching records 
    Set record1 = Range(c, c.offset(0, numCols)) 
    Set record2 = Range(col2.Cells(m,1), col2.Cells(m,numCols)) 
    ' now you call another function to compare these records and record the result 
    ' using the same trick as above to "go to the next line" - using output.Offset(1,0) 
    End If 
Next c 
End Sub 
1

您可以用公式做到這一點:

要GI已經你一個想法,基本上,如果你有在列兩份名單一& B,你可以使用公式像下面列C和d顯示匹配或不匹配:

在C1,

=If(isna(match(A1,B:B,0)),A1,"") 

,並且在D1

=IF(Isna(Match(B1,A:A,0)),B1,"") 

兩個複製下來。

延伸閱讀: