Sub tata()
Dim i&, j&, l&, m&, t, d(), e()
Dim ab1 As Range, ab2 As Range, c1 As Range, c2 As Range
With Me
l = .Cells(.Rows.Count, 1).End(xlUp).Row - 1
Set ab1 = .Cells(1, 1).Offset(1).Resize(l, 2).Cells
m = .Cells(.Rows.Count, 3).End(xlUp).Row - 1
Set c1 = .Cells(1, 3).Offset(1).Resize(m, 1).Cells
End With
With Application: .ScreenUpdating = 0: .EnableEvents = 0: .Calculation = -4135: End With
Sheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet
ab1.Copy Destination:=.Range("A2")
Set ab2 = .Cells(2, 1).Resize(l, 2).Cells
With .Sort
.SortFields.Add Key:=ab2.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange ab2
.Header = xlNo: .MatchCase = False: .Orientation = xlTopToBottom: .SortMethod = xlPinYin
.Apply
.SortFields.Clear
End With
d = ab2.Value
c1.Copy Destination:=.Range("E2")
Set c2 = .Cells(2, 4).Resize(m, 3).Cells
With c2.Cells(1, 1): .FormulaR1C1 = "nok": .AutoFill Destination:=c2.Columns(1), Type:=xlFillValues: End With
With c2.Cells(1, 3): .FormulaR1C1 = "1": .AutoFill Destination:=c2.Columns(3), Type:=xlFillSeries: End With
With .Sort
.SortFields.Add Key:=c2.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange c2
.Header = xlNo: .MatchCase = False: .Orientation = xlTopToBottom: .SortMethod = xlPinYin
.Apply
.SortFields.Clear
End With
e = c2.Value
j = 1
For i = 1 To m
t = e(i, 2)
Do While t > d(j, 1) And j < l: j = j + 1: Loop
If t = d(j, 1) Then e(i, 1) = d(j, 2)
Next
c2.Value = e
With .Sort
.SortFields.Add Key:=c2.Columns(3), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange c2
.Header = xlNo: .MatchCase = False: .Orientation = xlTopToBottom: .SortMethod = xlPinYin: .Apply
End With
c1.Offset(, 1).Value = c2.Value
Application.DisplayAlerts = False: .Delete: Application.DisplayAlerts = True
End With
Me.Activate
With Application: .Calculation = -4105: .EnableEvents = 1: .ScreenUpdating = 1: End With
End Sub