Fins closest larger and smaller to and input number from another sheet and copy entire row

Obedan

New Member
Joined
Jan 5, 2016
Messages
6
I do not have any experience with VBA coding and I need your help to start learning.

I have two different sheets, The first one include the input from the user and should get the output as well.
99866575e06ab581897472bebfa3784c34f3408788605ae705e3579123e8048c57c910fc.jpg


The second sheet has all the data.

633141974fda12acfbac73e0d2ae308c1918df2d1a99e5a6c9ea7da1dbcfbd95e1ee679a.jpg


The user should input two variables D and W in the first sheet , The W value is fixed and the user will choose it from drop down list.

The code should find 2 rows with the same input W value and closest larger and closest smaller with respect to the value of D. Then it should copy these entire rows from sheet 2 to sheet 1.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Is the second sheet always going to be in order like that? Sorted by Ascending D, then Ascending W, like it is in your example?
 
Upvote 0
What I'm thinking of is when the user choose a value of W for example 0.5, then the matching should lookup rows from 2 to 10 only looking for closest higher and lower match of D then copy the corresponding rows.

And if W value choosed as 0.7, the search should only be from rows 12 to 22

I hope this comment could help and make the problem clear.
 
Upvote 0
Ok here you go. Enter this into cell A8, use CTRL+SHIFT+ENTER, not just ENTER... and fill to the right all the way to F8:

=INDEX(Sheet2!A:A,MATCH($C$4,IF(Sheet2!$F:$F=$E$4,Sheet2!$B:$B),1))

Then enter this into cell A9, CTRL+SHIFT+ENTER, and fill to the right all the way to F9:

=INDEX(Sheet2!A:A,MATCH($C$4,IF(Sheet2!$F:$F=$E$4,Sheet2!$B:$B),1)+(LOOKUP($C$4,IF(Sheet2!$F:$F=$E$4,Sheet2!$B:$B))<>$C$4))
 
Upvote 0
Hi Obedan,

Sorry.. I had logged off for the day.

Here is the solution:-

Enter below formula for Closest smaller:-

=INDEX(Sheet2!A$1:A$22,MAX(IF((Sheet2!$B$1:$B$22<Sheet1!$C$4)*(Sheet2!$F$1:$F$22=Sheet1!$E$4),ROW(Sheet2!$A$1:$A$22),"")))

For closest larger:-

=INDEX(Sheet2!A$1:A$22,MIN(IF((Sheet2!$B$1:$B$22>Sheet1!$C$4)*(Sheet2!$F$1:$F$22=Sheet1!$E$4),ROW(Sheet2!$A$1:$A$22),"")))

Enter these formula as array formula (entered with key combination ctrl shift enter).

Regards,
DILIPandey
 
Upvote 0
Thank you very much both of you DILIPandey and Sevendiamond for your help.

It works with me.

This is really appreciated.

I have one more extra addition I would like to add.

let us assume we have W=0.5 and D=6 , then the smaller value will be D=5.5 , but there is no larger value ! in this case can we show an error to the user ?

Or something like that.
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

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