Formula to match the closest value and get corresponding description

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
310
Office Version
  1. 2019
Platform
  1. Windows
Hello,

<style type="text/css">
table.tableizer-table {
font-size: 12px;
border: 1px solid #CCC ;
font-family: Arial, Helvetica, sans-serif;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #CCC ;
}
.tableizer-table th {
background-color: #104E8B ;
color: #FFF ;
font-weight: bold;
}
</style>May someone help me with a formula if possible for the following table. I see that a normal MATCH function is not working here.

In column A I have the CC values and in column B corresponding description for each value (CODE).

In Column D I have a list of numbers for which I want to look for the closest value in column A and if found put in column E the corresponding description (CODE). The values in red are the output desired for the input NUMBERS in D.


CCCODENUMBERSCODE
237CMR18763044JAM
230MUS187635JAM
61AUS23092MUS
31NLD3162NLD
599ANT38050NOT FOUND
358FIN33FRA
33751FRA49185NOT FOUND
65SGP51078NOT FOUND
1721SXM1246BRB
1876JAM
81JPN
124622BRB

<tbody>
</tbody>

Thanks in advance for any help.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
In E2 control+shift+enter, not just enter, and copy down:

=IF($D2="","",IFERROR(LOOKUP(9.99999999999999E+307,SEARCH(IF(LEN($A$2:$A$13)>LEN($D2),"|"&LEFT($A$2:$A$13,LEN($D2)),"|"&$A$2:$A$13),"|"&$D2),$B$2:$B$13),"NOT FOUND"))
 
Upvote 0
Hello Aladin,

Thanks so much for your help. It works with sample data but when I test with actual data some issues appeared, for example if I have in column A a value with 1 and its CODE is USA, the output for the values that begin with 1 change all to USA and should be JAM and BRB for this sample. (In original sample I replace 81-JPN with 1-USA and highlighted in blue). With this a little bit modified sample the output should be the same as in first post, so the output values in red (USA) are incorrect.

May you help me to fix this. Thanks again.

CCCODENUMBERSCODE
237CMR18763044USA
230MUS187635USA
61AUS23092MUS
31NLD3162NLD
599ANT38050NOT FOUND
358FIN33FRA
33751FRA49185NOT FOUND
65SGP51078NOT FOUND
1721SXM1246USA
1876JAM
1USA
124622BRB

<tbody>
</tbody>
 
Last edited:
Upvote 0
Sort A12:B13 on CC in ascending order and check this move covers all or most of input variations.

Hello Aladin,

Thanks so much for the help. It seems to work perfectly in the actual data sorting column A as you said. I only have a doubt.

Normally I have stored the numbers as text for other tasks, in this case in order your formula works the numbers should be stored as numbers or as text? I ask this because the formula works perfect when column A is sorted ascending and the sorting gives different result when the values as stored as text or as numbers and because the values have no the same length.

Regards
 
Last edited:
Upvote 0
@ Fractalis

The formula does not expect numbers. Sorting in ascending order allows it to evaluate jon12 after jon1, 1001 after 102. So, sort your text numbers under the the sort regime: "Sort anything that looks like a number, as a number".
 
Upvote 0
@ Fractalis

The formula does not expect numbers. Sorting in ascending order allows it to evaluate jon12 after jon1, 1001 after 102. So, sort your text numbers under the the sort regime: "Sort anything that looks like a number, as a number".
Understood. Very appreciated your help and thanks for share your knowledge. We learn from this kind of solutions.

Regards
 
Upvote 0
Hello again Aladin,

Some time ago you helped me with this question and with my data of that moment all requirements were satisfied by your array formula.

Now all is the same, except I'd like to know if you could help me to add one condition to the formula.

I've highlighted in red in column CODE (current output) the values are different for my new condition. In column CODE(Expected) is the output desired.

The condition is that when the IF() evaluates to TRUE or FALSE, if there are more than one matches, then the CODE I like in output would be the first in ascending order. For example, for NUMBER 33 there are 3 matches (334/FRA3, 33750/FRA1, 33751/FRA2). The current output is selecting FRA2 but sorted ascending the 3 matches results that first would be FRA1.

I hope make sense.

Thanks in advance for any help.


COUNTRY CODECODENUMBERSCODE (CURRENT)CODE (EXPECTED)
1USA33FRA2FRA1
31NLD124BRB92BRB45
61AUS1246BRB92BRB7
65SGP1721SXM4SXM2
124BRB453162NLDNLD
230MUS23092MUSMUS
237CMR12462BRB92BRB5
334FRA349185NOT FOUNDNOT FOUND
358FIN51078NOT FOUNDNOT FOUND
599ANT187635JAMJAM
1246BRB73375012FRA1FRA1
1721SXM218763044JAMJAM
1721SXM4
1876JAM
33750FRA1
33751FRA2
124622BRB5
124623BRB8
124624BRB92

<tbody>
</tbody>


<style type="text/css"> table.tableizer-table { font-size: 12px; border: 1px solid #CCC ; font-family: Arial, Helvetica, sans-serif; } .tableizer-table td { padding: 4px; margin: 3px; border: 1px solid #CCC ; } .tableizer-table th { background-color: #104E8B ; color: #FFF ; font-weight: bold; }</style><style type="text/css"> table.tableizer-table { font-size: 12px; border: 1px solid #CCC ; font-family: Arial, Helvetica, sans-serif; } .tableizer-table td { padding: 4px; margin: 3px; border: 1px solid #CCC ; } .tableizer-table th { background-color: #104E8B ; color: #FFF ; font-weight: bold; }</style>
 
Last edited:
Upvote 0
Hello again Aladin,

Some time ago you helped me with this question and with my data of that moment all requirements were satisfied by your array formula.

Now all is the same, except I'd like to know if you could help me to add one condition to the formula.

I've highlighted in red in column CODE (current output) the values are different for my new condition. In column CODE(Expected) is the output desired.

The condition is that when the IF() evaluates to TRUE or FALSE, if there are more than one matches, then the CODE I like in output would be the first in ascending order. For example, for NUMBER 33 there are 3 matches (334/FRA3, 33750/FRA1, 33751/FRA2). The current output is selecting FRA2 but sorted ascending the 3 matches results that first would be FRA1.

I hope make sense.

Thanks in advance for any help.


COUNTRY CODECODENUMBERSCODE (CURRENT)CODE (EXPECTED)
1USA33FRA2FRA1
31NLD124BRB92BRB45
61AUS1246BRB92BRB7
65SGP1721SXM4SXM2
124BRB453162NLDNLD
230MUS23092MUSMUS
237CMR12462BRB92BRB5
334FRA349185NOT FOUNDNOT FOUND
358FIN51078NOT FOUNDNOT FOUND
599ANT187635JAMJAM
1246BRB73375012FRA1FRA1
1721SXM218763044JAMJAM
1721SXM4
1876JAM
33750FRA1
33751FRA2
124622BRB5
124623BRB8
124624BRB92

<tbody>
</tbody>


<style type="text/css"> table.tableizer-table { font-size: 12px; border: 1px solid #CCC ; font-family: Arial, Helvetica, sans-serif; } .tableizer-table td { padding: 4px; margin: 3px; border: 1px solid #CCC ; } .tableizer-table th { background-color: #104E8B ; color: #FFF ; font-weight: bold; }</style><style type="text/css"> table.tableizer-table { font-size: 12px; border: 1px solid #CCC ; font-family: Arial, Helvetica, sans-serif; } .tableizer-table td { padding: 4px; margin: 3px; border: 1px solid #CCC ; } .tableizer-table th { background-color: #104E8B ; color: #FFF ; font-weight: bold; }</style>

Control+shift+enter, not just enter, and copy down:

=IFERROR(IFERROR(INDEX($B$2:$B$20,MATCH("|"&D2&"*",IF(LEN($A$2:$A$20)>LEN($D2),"|"&LEFT($A$2:$A$20,LEN($D2))&"*","|"&$A$2:$A$20&"*"),0)),LOOKUP(9.99999999999999E+307,SEARCH(IF(LEN($A$2:$A$20)>LEN($D2),"|"&LEFT($A$2:$A$20,LEN($D2)),"|"&$A$2:$A$20),"|"&$D2),$B$2:$B$20)),"NOT FOUND")

which appears to fit the desired results you posted.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top