简体   繁体   English

加快列搜索

[英]Speed up search in column

In order to find specific values in a column and use the corresponding value in the next column, I use code below. 为了在列中查找特定值并在下一列中使用相应的值,我使用以下代码。 Although it works perfectly, I am looking for a faster way to do this search. 虽然效果很好,但我正在寻找一种更快的搜索方法。

However, the column I want to search is increasing in size as you can see in the first part of the code. 不过,我想搜索列于规模在不断扩大,你可以在代码的第一部分看到。

This works fast enough for columnsizes up to a few hundred values, but when it has more then 20000 values it is really slowing down. 这对于列大小最多几百个值的工作速度足够快,但是当它具有大于20000的值时,它的确会变慢。 Since I have to do this some ten times in my userform I would like to do this a lot faster. 由于我必须在用户表单中执行十次此操作,因此我想更快地执行此操作。

Public Function LR3() As Long   'Func.loc. HW08
    Worksheets("Sheetname").Activate
    Application.Volatile
    LR3 = Cells(Rows.Count, 1).End(xlUp).Row
End Function

For i = 1 To LR3
    If CStr(Sheet11.Cells(i + 1, 1).Value) = FunctionalLocation.Value Then
        Description.Value = Sheet11.Cells(i + 1, 2).Value
    Else
         'no action
    End If
Next i

use dictionary, try this 使用字典,试试这个

Sub test()
Dim Dic As Object, key As Variant, LR3&, oCell As Range
Set Dic = CreateObject("Scripting.Dictionary")
LR3 = Cells(Rows.Count, 1).End(xlUp).Row
For Each oCell In Sheet11.Range("A1:A" & LR3)
    If Not Dic.exists(oCell.Value) Then
        Dic.Add oCell.Value, oCell.Offset(, 1).Value
    End If
Next
For Each key In Dic
    If key = FunctionalLocation.Value Then Description.Value = Dic(key)
Next
End Sub

I assume the 2nd codefragment is copied out of a larger Sub, if so, it would be helpful to know what "FunctionalLocation" and "Description" are exactly. 我假设第二个代码片段是从较大的Sub中复制出来的,如果这样,那么知道“ FunctionalLocation”和“ Description”到底是什么会有所帮助。

But I guess this is what you want: 但是我想这就是你想要的:

Sheet11.Description.Formula = "=VLOOKUP(A2:B" & LR3 + 1 & ",2,false)"

Sheet11.Range("C2").AutoFill Destination:=Range("C2:C" & LR3 + 1 & ""), Type:=xlLinearTrend

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM