高效地识别并返回大型工作表中的缺失值

时间:2016-03-29 14:22:48

标签: excel excel-vba loops vba

我有700.000+行的大表(部件),其中部件属于具有唯一ID的计算机。

ID  |  Part  | Final Category  |  Part Cost Price

在另一张表(机器成本价格)中,我有一个包含机器ID的列表,我想检查其完整性(以确保我没有遗漏任何东西/什么都没有出错)。

ID  |  Total costs

如何有效地检查我是否在“机器成本价格”表中包含了“零件”表中的所有ID - 如果我错过了,请将其添加到底部?

我已经尝试了一个foreach循环,但是这导致我的excel立即崩溃。

任何帮助都会受到关注:)

1 个答案:

答案 0 :(得分:0)

由于您的要求是多部分:

  1. 检查机器清单以确保所有机器都在零件清单中列出
  2. 检查零件清单以确保所有机器都在机器清单中显示
  3. 第一部分可以用表格公式轻松解决,所以我将重点关注第二部分for each循环失控。

    要解决,我会:

    将700000台机器的列表复制到新选项卡:

    Sheets("Parts").Range("A1:A700000").copy Destination:=Sheets("newtab").range("A1")
    

    使用.RemoveDuplicates对象的Range方法中内置的Excel格式对列表进行重复数据删除:

    Sheets("newTab").Range("A1:A700000").RemoveDuplicates(1,xlNo)
    

    现在你可以For Each这个更小的范围,或者你可以在那里抛出一个公式。

    Dim machineCell As Range
    
    'Loop through each cell in the dedup range
    For Each machineCell In Sheets("newtab").Range("A1:A" & Sheets("newtab").Range("A900000").End(xlUp).Row()).Cells
    
        'using the .find method of the range object, check for the machine value in the machine worksheet
        Set test = Range("A1:A5").Find(machineCell.Value)
        If Not test Is Nothing Then
            'If test is a range that is set, then we found it.
            'So update the column next to our machine cell to "found"
            machineCell.Offset(0, 1).Value = "found"
        End If
    Next machineCell