匹配两个excel文档中的行

时间:2016-04-15 21:52:35

标签: excel

我正在处理excel中的两个大型csv文件(每个大约100-400 MB),其中一个需要分成两个文档(D1和D2)才能打开它。拨打另一个D3。除了标题之外的这些文档中的每一行都具有形式ID,Data1,Data2,Data3 ...... Data 10左右,文档D1 / 2具有与D3不同的数据。我想将文档D1和D2中的信息添加到D3中,但仅适用于D3中存在的ID(较小的文件)。对我来说最简单的方法是什么?如果我可以在不打开excel文件的情况下执行此操作,我还有D1 / 2文件。

很抱歉,如果这是一个令人困惑的问题,我可以在必要时回答澄清问题

1 个答案:

答案 0 :(得分:0)

我在这里有一些可以帮助你入门的东西,请看一下,让我知道你的数据合并的逻辑......

Sub MergeData()

Dim D1 As Worksheet
Dim D3 As Worksheet
Dim Rng As Range
Dim Arr1 As Variant
Dim Arr2 As Variant
Dim Rw1 As Long, Rw2 As Long


'IF you the master file and 1 of the other files first in excel
'you can just reference them like this, otherwise change this accordingly
Set D1 = Workbooks("CSV1").Sheet1
Set D3 = Workbooks("CSV3").Sheet1


'D3 is the masterFile
'Lets store its data in Arr1
Set Rng = D3.UsedRange
Arr1 = Rng.Value

'since your working on large files lets try keep memory usage down to a minimum
'we will work on 2 sheets at a time not all 3.
'Arr2 now contains the sheet (CSV1) we will extract data from
Set Rng = D1.UsedRange
Set Arr2 = Rng.Value

Set Rng = Nothing

'Notice the + 1, its to skip the headers in case your wandering
'This is the basic loop setup
For Rw1 = LBound(Arr1, 1) + 1 To UBound(Arr1, 1)
    For Rw2 = LBound(Arr2, 1) + 1 To UBound(Arr2, 1)
        If Arr(Rw1, 1) = Arr2(Rw2, 1) Then  'OR  lcase(Arr(Rw1, 1)) = lcase(Arr2(Rw2, 1)) if letter case might be a problem
            'we found a matching ID
            'now im not sure exactly what you want to happen here.....
            'do you want to check if the master file has all the data that the other file has
            'if not then transfer the columns of data it is missing
            'or should all the columns in the master file be updated with the new values in the other file.....
            'This will change what code needs writing here
            'we may need an aditional array to hold new values temporarily
        End If
    Next Rw2
Next Rw1

'After all data has been extracted
'this code will also depend on what happens above
'but something like this
'set rng = D1.Range("this will be the size of the array").Value = Arr1

End Sub

完全相同的代码将用于第二个文件,因此我只启动了2个文件。

我们需要更多关于要提取的数据的输入,以及我们是否正在替换主文件等中的任何内容,具体而言。