如何更新从VBA字典表没有性能问题?

问题描述 投票:0回答:1

我有一个相当大的电子表格(片A)(400K行),都带有唯一标识符。

我有这些〜150K在另一电子表格(表B),其需要传送其值。要做到这一点,我各个ID存储在字典和其值在其值的阵列,以进行更新。例如。

dict.Add 'ID_01', Array('val1', 'val2', 'val3')

然后更新,我通过片材A和运行每当遇到该ID时,它更新细胞。对于一个粗略的想法:

With sheet A
For i = 2 to lastrow:
    If dict.exists(.Cells(i, 1).value) Then
        .Cells(i, 2).Value = dict.Item(.Cells(i,1).Value)(0)
        .Cells(i, 3).Value = dict.Item(.Cells(i,1).Value)(1)
        .Cells(i, 4).Value = dict.Item(.Cells(i,1).Value)(2)
Next i    

结束与

以上只是一个模拟的例子,但你可以看到为什么这可能需要很长的时间,通常10分钟以上。瓶颈不从片B添加的ID /值对的字典,而是更新它们放回表A.

我的问题是:有没有办法让我更快地更新这些数值,同时仍保留我的字典结构?

任何帮助表示赞赏,但它会是最好,如果任何提出的解决方案并没有从我有上述的一般做法离题太远。

excel vba
1个回答
3
投票

创建并填充在存储器中的2-d阵列然后转储阵列回工作表。

'at this point the dictionary is already populated similar to
'dict.Add 'ID_01', Array('val1', 'val2', 'val3')

dim i as long, arr as variant

with sheet a

    arr = .range(.cells(2, "A"), .cells(.rows.count, "A").end(xlup)).value2
    redim preserve arr(lbound(arr, 1) to ubound(arr, 1), 1 to 4)
    'if sheet A already has values that only require updating,
    'then use this instead
    'arr = .range(.cells(2, "A"), .cells(.rows.count, "A").end(xlup).offset(0, 3)).value2

    for i=lbound(arr, 1) to ubound(arr, 1)
        If dict.exists(arr(i, 1)) Then
            arr(i, 2) = dict.Item(arr(i, 1))(0)
            arr(i, 3) = dict.Item(arr(i, 1))(1)
            arr(i, 4) = dict.Item(arr(i, 1))(2)
        end if
    next i

    .cells(2, "A").resize(ubound(arr, 1), ubound(arr, 2)) = arr

end with
© www.soinside.com 2019 - 2024. All rights reserved.