Nearest Match on descending scale

RojParody

New Member
Joined
Mar 22, 2011
Messages
10
Good morning,

Been lurking for a while and managed to uncover some great answers to things I needed, but can't seem to find anything for this question.

I have a table with four columns (Code; City; Site; Group). The "Code" column contains a list of numbers, ranging from 3-digit numbers to 11-digit numbers.

I am trying to search for the match for an 11-digit number, matching 11-digits if possible, if not then matching the first 10, then first 9, on a sliding scale.

For example, if I have the following:

Code City Site Group

0123 City 1 Site A Group A
012345 City 2 Site B Group A
01234567 City 3 Site C Group B

I want to search for a match to 01234567890, and return the Site, for example:

01234567 City 3 Site C Group B = "SITE C"

I don't know if this is possible, but thought I'd ask before giving up altogether! :)

Roj
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Welcome to MrExcel.

Given your sample data and 01234567890 in E1, you can use:

=VLOOKUP(E1,A2:C4,3)

The formula assumes that all the numbers are real numbers formatted to display leading zeroes.
 
Upvote 0
Thank you Andrew, but this isn't coming up with the correct return. It's searching the table, and coming up with something based on a different row.

I have attached an example pic below of the table I have.

In this instance, I need the formula to search F2, and return "95" as an outcome (based on the number in F2 being the closest match to row 11).

188799_10150468981980145_863280144_17863363_3055_n.jpg
 
Upvote 0
Thank you Andrew, but this isn't coming up with the correct return. It's searching the table, and coming up with something based on a different row.

I have attached an example pic below of the table I have.

In this instance, I need the formula to search F2, and return "95" as an outcome (based on the number in F2 being the closest match to row 11).

188799_10150468981980145_863280144_17863363_3055_n.jpg

Apologies for the sloppiness... I had taken the screen shot before I sorted column A by number order - however it still didn't bring up what was wanted.
 
Upvote 0
How about
=INDEX(C2:C12, MATCH(MIN(ABS(LEFT(A2:A12&REPT("0",11), 11)-LEFT(F2&REPT("0",11), 11))), ABS(LEFT(A2:A12&REPT("0",11), 11)-LEFT(F2&REPT("0",11),11)), 0), 1)

Entered with Ctrl-Shift-Enter (Cmd+Return for Mac)
 
Upvote 0
Hi Mike,

Thank you for that, it's getting closer!

Here's another screen shot with you, with your formula included.

189652_10150469044820145_863280144_17863797_6153874_n.jpg


In this example, the outcome in G2, G3 and G4 are correct.

However, the outcome of G5 should be 95 (If you go back, the first exact match to the start of the number is in A7); and G6 should 28 (again, first exact match to the start is in A6)

Is this possible?
 
Upvote 0
Try replacing the two instances of
ABS(LEFT(A2:A12&REPT("0",11), 11)-LEFT(F2&REPT("0",11), 11))

with

IF(LEFT(A2:A12&REPT("0",11), 11) < LEFT(F2&REPT("0",11), 11), LEFT(A2:A12&REPT("0",11), 11)-LEFT(F2&REPT("0",11), 11))
 
Upvote 0
Seems to simple after looking at Mike's suggestion, but maybe

In E2 =--(A2&REPT(0,11-LEN(A2)))

Fill down and sort data by column E, ascending values

In G2 =INDEX(C:C,MATCH(F2,E:E))

Will fail if you have the helper formula in column E with an empty cel in column A
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,671
Members
452,937
Latest member
Bhg1984

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