0

我在这里遇到了严重的问题..任何形式的帮助都非常感谢!

我有两个巨大的文本文件(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
4

2 回答 2

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
于 2013-05-11T20:44:42.323 回答
1
于 2013-05-13T08:46:51.343 回答