在数组内部查找或在Excel中快速查找

时间:2014-05-22 13:19:29

标签: arrays excel vba excel-vba

我有两个范围

工作表(1)中从第二行到最后一行的第一个范围(“D:D”),这是Lookup_values,140.000行

x1 = Worksheets("1").range(Worksheets("1").Cells(2, "D").Address, Worksheets("1").Cells(Rows.Count, "D").End(xlUp)).value

工作表(2)中的第二个范围(“A:D”)从第二行开始到最后一行,这是tabble_array,500.000行

x2 = Worksheets("2").range(Worksheets("2").Cells(2, 4).Address, Worksheets("2").Cells(Rows.Count, 1).End(xlUp)).value

这里我试图在数组内部查找

ReDim ListBoxArrSplitToRows(1 To 4, 1 To UBound(x2, 1))
CX = UBound(x2, 2)
For ii = 2 To UBound(x1, 1)
    For i = 1 To UBound(x2, 1)
        SearchInst = x2(i, 1)
        txt = x1(ii, 1)
        If InStr(SearchInst, txt) Then
            zz = zz + 1
            For counter = 1 To 4
                ListBoxArrSplitToRows(counter, zz) = x2(i, counter)
            Next counter
        Else
        End If
    Next i

Next ii

    If zz <> 0 Then ReDim Preserve ListBoxArrSplitToRows(1 To 4, 1 To zz) Else ReDim ListBoxArrSplitToRows(0, 0): MsgBox "No matches"

Worksheets(1).Cells(2, "E").Resize(UBound(ListBoxArrSplitToRows, 2), 3) = ListBoxArrSplitToRows

ii=3 Ubound(x1,1) = 136586 Ubound(x2,1) = 496369 zz=1

如何快速查找两个大范围,因为代码需要30分钟来查找值并且它太长了

1 个答案:

答案 0 :(得分:0)

据我所知,你的&#34;下标的原因超出范围&#34;错误是zz大于UBound(x2, 1),使其超出ListBoxArrSplitToRows范围。

快速解决方法是移动此行

If zz <> 0 Then ReDim Preserve ListBoxArrSplitToRows(1 To 4, 1 To zz)

高一线,即Next i之前。虽然摆脱Else ReDim ListBoxArrSplitToRows(0, 0);我不知道这会达到什么目的。