|
楼主 |
发表于 2023-2-7 19:56
|
显示全部楼层
针对于sheet2,完善了一下代码,发觉有点啰嗦,看哪个大神能优化一下
Sub 最接近的值查询()
Sheet2.Activate
Dim Arr, Brr, Str1$, Str2$, x, y
Dim dic As Object
Set dic = CreateObject("scripting.dictionary")
Arr = Range("A1").CurrentRegion
[I2:I10] = ""
Brr = Range("F1").CurrentRegion
For i = 2 To UBound(Brr)
For j = 2 To UBound(Arr)
Str1 = Brr(i, 1) & Brr(i, 2)
Str2 = Arr(j, 1) & Arr(j, 2)
If Str1 = Str2 Then
dic(Str1) = dic(Str1) + 1
If dic(Str1) = 1 Then
x = Arr(j, 4)
y = Abs(Arr(j, 3) - Brr(i, 3))
End If
If Abs(Brr(i, 3) - Arr(j, 3)) < y Then
y = Abs(Arr(j, 3) - Brr(i, 3))
x = Arr(j, 4)
End If
End If
Next
Brr(i, 4) = x
x = Empty
Next
Range("F1").Resize(UBound(Brr), 4) = Brr
End Sub
|
|