VBA
Sub test()
Dim a, e, i As Long, ii As Long, w, temp, dic As Object
Set dic = CreateObject("Scripting.Dictionary")
a = Sheets("sheet2").Cells(1).CurrentRegion.Value
For i = 3 To UBound(a, 1)
If Not dic.exists(a(i, 1)) Then
ReDim w(1 To 3, 1 To 1)
Else
w = dic(a(i, 1))
ReDim Preserve w(1 To 3, 1 To UBound(w, 2) + 1)
End If
For ii = 2 To 4
w(ii - 1, UBound(w, 2)) = a(i, ii)
Next
dic(a(i, 1)) = w
Next
With Sheets("sheet1").Cells(1).CurrentRegion.Offset(1)
a = .Value
For i = 1 To UBound(a, 1) - 1
temp = a(i, 1): a(i, 1) = Empty
If dic.exists(temp) Then
For ii = 1 To UBound(dic(temp), 2)
If (a(i, 2) >= dic(temp)(1, ii)) * (a(i, 2) <= dic(temp)(2, ii)) Then
a(i, 1) = dic(temp)(3, ii): Exit For
End If
Next
End If
Next
.Columns(3).Value = a
End With
End Sub
Bookmarks