我目前有一个查找表,我用来为一个记录分配一个值,给定3个输入,以便在分配值之前进行匹配。该表有3426条记录,我希望找到一种方法来使查找过程更快地运行。我正在使用以下函数来执行查找:
Function FindValue(rng1 As String, rng2 As Date, rng3 As String) As Variant
Dim varVal1 As Variant
Dim varVal2 As Variant
Dim varVal3 As Variant
Dim rngTargetA As Range
Dim rngTargetB As Range
Dim rngTargetC As Range
Dim lngRowCounter As Long
Dim ws As Worksheet
varVal1 = rng1
varVal2 = rng2
varVal3 = rng3
Set ws = Sheets("Uncertinaty_LU")
lngRowCounter = 2
Set rngTargetA = ws.Range("A" & lngRowCounter)
Set rngTargetB = ws.Range("B" & lngRowCounter)
Set rngTargetC = ws.Range("C" & lngRowCounter)
Do While Not IsEmpty(rngTargetA.Value)
If UCase(rngTargetA.Value) = varVal1 And rngTargetB.Value = varVal2 And UCase(rngTargetC.Value) = UCase(varVal3) Then
FindValue = ws.Range("D" & lngRowCounter).Value
Exit Function
End If
lngRowCounter = lngRowCounter + 1
Set rngTargetA = ws.Range("A" & lngRowCounter)
Set rngTargetB = ws.Range("B" & lngRowCounter)
Set rngTargetC = ws.Range("C" & lngRowCounter)
Loop
' if we don't find anything, return an empty string '
MsgBox ("Could not find uncertainty value for " & varVal1 & ", " & varVal2 & ", " & varVal3)
FindValue = ""
End Function
在上面的函数rng1
中,rng2
和rng3
是来自工作簿中其他页面的输入。
虽然查找表中有3426条记录,但rngTargetA
中有38个唯一值,rngTargetC
有4个唯一值。我希望找到一个解决方案,它首先在rngTargetA
中找到感兴趣的唯一值,然后才开始循环记录以查找感兴趣的记录。