+ Reply to Thread
Results 1 to 16 of 16

Find closest value from Range

  1. #1
    Forum Contributor
    Join Date
    12-24-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    153

    Find closest value from Range

    Dear Excel Gurus,

    I had created a simple macro with looping in columns C,D,E by formulas. In column H it returns "0" or "1" depends on 2 conditions in columns F,G. I tried my best here

    Please help me to find value in column C, which is closest to "1" always IF in column H also "1". On next step just return corresponding value from column B.

    Any optimizations and improvements to my code are only welcome. For me the best is to avoid additional columns F, G, H and have all calcs inside the code. (my file is attached)

    I would be appreciate for any input. Thx.

    Code:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Find closest value from Range

    Rather than trying to reverse engineer your code would you explain in a simple narrative form what your data represents, what you are trying to achieve and all the rules and conditions you apply.

    Your note in the spreadsheet only refers to columns H&C no mention of the other columns. It's also confusing since you describe column H as the final (result presumably), but then the one 'rule' you do desccribe says IF Column H blah blah. If H is a result how can it be used to manipulate other data unless you're implying some sort of circular iteration is going on?
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    12-24-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    153

    Re: Find closest value from Range

    Hello!

    You are right, reverse iteration only for my testing...of course it can start from i = 1. No problem, I will try to explain the logic: column B contain always input data, eg for i=1 macro takes value from cell B2 --> calc C2 --> calc D2 -->calc E2 (by integrated formulas), then check two conditions: if D2>=10^4 then F2=1 else 0, IF E2>=1.15 then G2=1 else 0. Last calc in column H is logical AND of columns F and G --> next row i+1.

    Final goal - is to check full column H, if it's contains "1", after that return closest value as I described before. So, FINAL RESULT should comes from column B based on column H (if=1) and C (value closest to "1" always)

    I hope now this task is more clear for you.

    Thank you for support!

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Find closest value from Range

    Can you give example of the final result ?
    You still need to display columns C,D,E ??
    Last edited by PCI; 01-05-2020 at 03:08 PM.
    - Battle without fear gives no glory - Just try

  5. #5
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Find closest value from Range

    Why not a formula
    =MIN(ABS(1-C2:C16))+1
    Confirm using Control + Shift + Enter for array formula, some curly brackets will appeared at the neginning end at the end
    See file attached
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    12-24-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    153

    Re: Find closest value from Range

    In reality no, I just need to iterate by column B and find final result.

  7. #7
    Forum Contributor
    Join Date
    12-24-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    153

    Re: Find closest value from Range

    Thank you, but i need solution by macro. How to find by formulas is completely clear for me

  8. #8
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Find closest value from Range

    Is it what you need
    Result in cell K2
    See file attached

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by PCI; 01-05-2020 at 04:04 PM. Reason: Code updated

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Find closest value from Range

    Same macro with only column "B"
    BTW There a button so a macro was prepared, but we could do also using and UDF ...!
    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Find closest value from Range

    The same with UDF see file attached
    K2=ClosestTo1(B2:B16)

    Please Login or Register  to view this content.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    12-24-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    153

    Re: Find closest value from Range

    Awesome, my friend!!! Everything works as should be, shake your hand

  12. #12
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Find closest value from Range

    Good news for a new year
    You're welcome

  13. #13
    Forum Contributor
    Join Date
    12-24-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    153

    Re: Find closest value from Range

    Sorry, i forgot one thing: how I can return corresponding value from column B? In that case it should be = 38.4

  14. #14
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Find closest value from Range

    Here you are

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    12-24-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    153

    Re: Find closest value from Range

    Brilliant!!! thank you so much

  16. #16
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Find closest value from Range

    You're still welcome, it was not to difficult ...!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find closest value in range with ascending then descending data
    By NatalieEC in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-22-2018, 07:17 PM
  2. How to find the closest number in range
    By mariab1 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 12-11-2015, 10:52 AM
  3. Replies: 1
    Last Post: 11-02-2012, 08:32 AM
  4. [SOLVED] Find the closest range of numbers and their cross values
    By Alexnf in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-15-2012, 04:12 AM
  5. Replies: 13
    Last Post: 03-27-2012, 05:12 PM
  6. find closest date (number) in a range
    By wamp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-23-2010, 01:50 PM
  7. Find closest value in a range
    By msbing916 in forum Excel General
    Replies: 2
    Last Post: 11-19-2009, 11:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1