比较使用vba的巨大的文本文件

我在这里得到严重的问题..任何forms的帮助,非常感谢!

我有两个巨大的文本文件(130 MB),每个都有数千个logging。 我需要使用vba或任何方式比较这两个文件,并生成一个电子表格,其中包括标题和两个额外的列。 这两个附加列将是文件名,在下一列中应该显示哪个列是错误的。 每个logging将有多个差异。 一个文件可以具有在其他文件中找不到的logging。 所以这个条件也应该logging在电子表格中。

例:

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

在上面的例子中,logging来自两个文件。 突出显示的是logging之间的差异。 所以输出应该是这样的..

 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 

看来这里有三个问题:

1)find两个文件之间的匹配logging(第一列)。

2)比较第一列匹配的logging – 如果有差异,logging其差异

3)如果logging存在于一个文件中而不是另一个,请logging该文件。

我将假设这两个“巨大的文件”实际上是在同一个Excel工作簿中的单独的工作表,并且logging按第一个键sorting。 这将大大加快处理速度。 但是,我认为,速度是次要的问题。 我还假设有第三张表格,你把输出。

这里是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 

你可以用公式做到这一点:

看到

  • MS KB:使用Excel来比较两个数据列表
  • 我Excel.com – 创build一个不匹配值的列表
  • ExcelExperts.com – 从第三列中的两列中提取不匹配的条目

为了给你一个想法,基本上,如果在列A和列B中有两个列表,可以在列C和列D中使用下面的公式来显示匹配或不匹配:

在C1中,

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

在D1中

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

都抄下来。

进一步阅读:

  • Excel索引函数和匹配函数 – 上下文MVP
  • Excel VLOOKUP和索引与匹配 – Excel用户MVP
  • Excel用户MVP – Excel的最佳查找方法:INDEX-MATCH