excelexcel-formulaexcel-2019

Look for values and show related codes in other range


I want to search the CODE for each value in column D. For this I´ve been able to get the formula in E2, getting the output in column E.

=INDEX($A$1:$A$16,MATCH(D2&"*",$B$1:$B$16,0))
A B C D E F G H
1

CODE

NUM

NUM

CODE

CODE1

CODE2

2 MMSPL 1312314 7965 FGDYY HHSWA HHSKQ
3 MMSPL 1404725 120631 HHSWA FGDYY FGD95
4 HHSWA 120631 33339145 HHSKQ FGDRO FGDLV
5 HHSKQ 33339145 1404725 MMSPL MMSPL MMSQH
6 HHSWA 316540 5275497 FGDRO
7 HHSWA 3460643 233663 FGDRO
8 FGDYY 7965 33339100 FGDLV
9 FGDYY 796660 796660 FGDYY
10 FGD95 33339183
11 FGDYY 7967
12 FGDRO 5199799
13 FGDRO 233663
14 FGDLV 33339100
15 FGDRO 52551223
16 FGDRO 5275497

My goal is (For Excel 2019):

If in matches NUMs, there is a related CODE2 as output, then for those NUMs that are related with CODE1, show as output CODE2 intead of CODE1 as shown in "desired output" in image below. how to do this? I hope make sense. Thanks

enter image description here


Solution

  • This approach checks if the looked-up CODE (from column A) exists in column G and, if so, checks if the VLOOKUP of that CODE, in columns G & H, is equal to the result below, in which case the result below is returned - otherwise the looked-up code from column A is returned: Screenshot illustrating suggested formula The formula in E2 is

    =IF(COUNTIF($G$2:$G$5,INDEX($A$1:$A$16,MATCH(D2&"*",$B$1:$B$16,0))),IF(VLOOKUP(INDEX($A$1:$A$16,MATCH(D2&"*",$B$1:$B$16,0)),$G$2:$H$5,2,FALSE)=E3,E3,INDEX($A$1:$A$16,MATCH(D2&"*",$B$1:$B$16,0))),INDEX($A$1:$A$16,MATCH(D2&"*",$B$1:$B$16,0)))
    

    which is then copied down.

    Since the INDEX() function is effectively calculated 4 times for each result, it would be more efficient to employ a helper column, such that the INDEX() result could be calculated just once per row, and merely referenced 4 times.