excel - Multiple Column lookup and return corresponding value in next column (nearest value) -
here's trying accomplish, have 2 sheets:
reference sheet: click see image
code length width height 78 48 25 b 78 48 34 c 12 7.4 5 d 12 15 5 e 12 15 7.5 f 12 15 9 g 24 15 5 h 24 15 7
solution sheet:
length width height returning code match_l match_w match_h 10 6 8 c 12 7.4 5
the formula in column "returning code" should nearest value in corresponding reference sheet i.e., length <-> length, width <-> width, height <-> height , return matching "code" corresponding row.
it have been simpler if want match when values equal in case, looking nearest value (either greater or lower) in each of corresponding columns , return matching "code" , values in match_l, match_w, match_h columns.
any or pointers highly appreciated!
following vba job.
sub lookupnearestvalue() dim ws worksheet: set ws = worksheets("sheet1") dim lastrow long: lastrow = ws.usedrange.find("*", searchorder:=xlbyrows, searchdirection:=xlprevious).row dim long, rowcounter long: rowcounter = 2 dim trowcounter long dim tvalue long dim tempvalue long dim tlength long, twidth long, theight long dim templength long, tempwidth long, tempheight long tlength = ws.cells(2, 6) twidth = ws.cells(2, 7).value theight = ws.cells(2, 8).value ws = 2 lastrow templength = ws.cells(rowcounter, 2) tempwidth = ws.cells(rowcounter, 3).value tempheight = ws.cells(rowcounter, 4).value tempvalue = abs(tlength - templength) + abs(twidth - tempwidth) + abs(theight - tempheight) if rowcounter = 2 tvalue = tempvalue trowcounter = rowcounter elseif rowcounter > 2 , tempvalue < tvalue tvalue = tempvalue trowcounter = rowcounter end if rowcounter = rowcounter + 1 next ws.cells(2, 9) = ws.cells(trowcounter, 1) ws.cells(2, 10) = ws.cells(trowcounter, 2) ws.cells(2, 11) = ws.cells(trowcounter, 3).value ws.cells(2, 12) = ws.cells(trowcounter, 4).value end end sub
to make macro work, need have data on sheet according these columns arrangement:
in sheet, have setup run macro on value change event in h2
cell.
Comments
Post a Comment