# Lookup data based on values in another column

ValueFromNearestYear.xlsxThe values of the "cbd" and "cc" fields highlighted in yellow and green correspond with a particular year of data as indicated by the number following the underscore in each column heading.

I am hoping that the values for columns C and D can be automatically populated based on the values from the yellow "cbd" and green "cc" columns, respectively, based on the year (or years) of data that are nearest to the value for "year" (column B) in the associated row.

The first four rows of data have been entered manually to show examples of how the nearest single value (or average of the two nearest values) should be obtained.
LVL 1
###### Who is Participating?

Older than dirtCommented:
Try this. It will handle new 'yellow' and 'green' years when you add them in the future.  I also froze the first row to make looking at the data easier. Click the blue button.
29094062.xlsm
1

Excel & VBA ExpertCommented:
0

Author Commented:
Thanks for letting me know. The file is now attached.
0

Older than dirtCommented:
Can you give an example of how more than one year could be involved?
0

Older than dirtCommented:
Also at some point in the future will there be more than 5 yellow and 5 green columns?
0

Author Commented:
More than one year would be involved only in the situation where the value for year in Column B is equal distance between two years in the colored columns. For example, row 3 has year = 2011 so the values needed in Columns C and D are derived as the average of the "cc" and "cbd" values from 2010 and 2012
0

Author Commented:
Yes, there would be the potential for additional years to be added. Although, not in the immediate future.
0

Excel & VBA ExpertCommented:
Maybe this...

In C2
``````=INDEX(E2:I2,MATCH(MIN(INDEX(ABS(MID(\$E\$1:\$I\$1,5,4)-B2),)),INDEX(ABS(MID(\$E\$1:\$I\$1,5,4)-B2),),0))
``````
and then copy it down.

In D2
``````=INDEX(J2:N2,MATCH(MIN(INDEX(ABS(MID(\$J\$1:\$N\$1,4,4)-B2),)),INDEX(ABS(MID(\$J\$1:\$N\$1,4,4)-B2),),0))
``````
and then copy it down.
0

Author Commented:
This looks close, but it doesn't seem to be doing the averaging when needed.

For example, cell D6554 should have a value of 27.5 (average of 55 from Column L and 0 from Column M)
0

Older than dirtCommented:
0

Author Commented:
Works great, many thanks!
0

Author Commented:
The macro included with the Best Solution solved the problem
0

Older than dirtCommented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
Experts Exchange Most Valuable Expert (MVE) 2015, 2017
Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
Experts Exchange Top Expert VBA (current)
0

Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.