根据另一个数组中的值在数组中插入元素

时间:2016-03-14 17:42:19

标签: arrays vba

我有两个非常大的文件需要在它们之间传递信息。目前,我循环遍历两个文件中的较大文件并使用数组来存储较小的文件。然后,我使用数组中的元素更新较大文件中的字段,这可以工作但循环文件需要很长时间。我想要做的是将两个文件放在单独的数组中并循环遍历数组,并将元素从一个数组插入到另一个数组中,就像我对文件一样。

我可以找到我正在寻找的东西我很难插入这个名字。

'loop through first Array
For X = UBound(arrOne) To 2 Step -1

    'loop through second array
    For A = UBound(arrTwo) To LBound(arrTwo) Step -1

        'check if the primary key in the first array is in the second array
        If arrOne(X, 1) = arrTwo(A, 1) Then

            'if the keys match find the secondary key 
            'AND make sure the name field is empty
            If arrOne(X, 69) = arrTwo(A, 2) And arrOne(X, 83) = "" Then

               'if keys match and there is no name - insert the name 
               'from arrTwo into arrOne
                arrOne.Add(X, 69) arrTwo(A, 2)

            End If

        Else

            'Insert Empty Values into Insurance rows

        End If
    Next A
Next A

1 个答案:

答案 0 :(得分:0)

数组没有Add方法。如果您只想将一个数组中的值分配给另一个数组中的一个插槽:

 arrOne(X, 69) = arrTwo(A, 2)

目前尚不清楚该行的目的是什么,因为您已经检查过这两个值是否相等?

如果您只期望一次匹配,那么在该行之后添加Exit For会加快您的处理速度。