Good time of day. There is a task in Excel. It is necessary to compare two columns and select the closest value from the first for the second column.
Macro which I use
1 St; 2 St; result
1200; 1900; 1300
1300; 1200; 1200
2800; 2680;
840; 841; 840
830; 720;
750;
all the time different data. Desirable macro.
Applied a macro, but it does not correctly select values.
Sub Raspredlitel() Dim arrEt, arrMetka, arrMetka2, arrIncome Dim i%, iGlob%, j%, eps&, mJ%, mI% arrEt = Range([D4], [D10000].End(xlUp)).Value arrIncome = Range([K4], [K10000].End(xlUp)).Value ReDim arrMetka(1 To UBound(arrEt)) ReDim arrMetka2(1 To UBound(arrIncome)) For iGlob = 1 To UBound(arrIncome, 1) eps = 9 ^ 9 mJ = 1 mI = 1 For i = 1 To UBound(arrIncome, 1) If IsEmpty(arrMetka2(i)) Then For j = 1 To UBound(arrEt) If Abs(arrIncome(i, 1) - arrEt(j, 1)) < eps And IsEmpty(arrMetka(j)) Then mJ = j mI = i eps = Abs(arrIncome(i, 1) - arrEt(j, 1)) End If Next j End If Next i arrMetka(mJ) = 1 arrMetka2(mI) = 1 [M3].Offset(mI) = arrEt(mJ, 1) Next iGlob End Sub