+ Reply to Thread
Results 1 to 8 of 8

Loop through table and find max number of condition fulfillment

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

    Loop through table and find max number of condition fulfillment

    Dear Excel Gurus,

    I faced to new complicated task. I need to create VBA macro which can loop through each row in table, on each row check condition fulfillment (except current row) and find maximum number of this cases. All details and my comments you can find in attached file.

    Any input is highly appreciated. Thanks in advance

    BR,
    Igor
    Attached Files Attached Files

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Loop through table and find max number of condition fulfillment

    Hi Igor,

    But ... have you tried to find just 1 such situation in your file?
    вы не найдете

    So either the conditions as described are wrong, or were written with error.

    To test I changed row 44 data to:
    43 5190 5200
    and then output is Unit9
    as for i = 9
    there is B10 = 5195
    and it is between
    B44 and C44


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Kaper; 10-10-2018 at 12:11 PM.
    Best Regards,

    Kaper

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

    Re: Loop through table and find max number of condition fulfillment

    Quote Originally Posted by Kaper View Post

    To test I changed row 44 data to:

    and then output is Unit9
    as for i = 9
    there is B10 = 5195
    and it is between
    B44 and C44
    Dear Kaper, thank you for reply.

    In this case should be listed Unit9 (i=9) AND Unit7 (i=43). This condition valid in all other cases: add current Unit to found Units.

    Sorry, I was in a hurry yesterday when creating a new thread...and forgot about main remarks from my side:
    1) During looping IF cells B(i) AND C(i) = 0, then skip this row
    2) Total number of cell B(i) AND C(i) can differ from time to time...so, macro should automatically find last value and run through whole table.
    3) Could you please list chosen units to current sheet, eg from cell [F2...Fn]?!

    I had entered new data in your file and receive error. Could you please adapt it?

    Best Regards,
    Igor
    Attached Files Attached Files

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Loop through table and find max number of condition fulfillment

    Hi:

    As for point 2 - it's already there array my_arr
    is loaded with values from B2 to Dn, where n is last filled row
    Please Login or Register  to view this content.
    it's like you select D1048576 and press Ctrl+UpArrow
    Try it. select D1048576 and press Ctrl+UpArrow. As you can see with your file - selection is now D57 - last filled cell in column D.

    Point 1 does not matter: there are zeros or positive values in columns B and C. and if Bx is 0 then Cx is also 0. So while looping there will never be situation that Cy>Bx>By for any y because there is no By <0. (every Bi is >=0). Also there will be no Cx>By>Bx because with Bx = 0 Cx is also = 0 so there will be no value whic could be bigger than 0 and smaller than 0.

    Point 3 is easy, but only once there is data which fulfills requirement
    But your data do not.

    The code could be:
    Please Login or Register  to view this content.
    But ... there is no data fulfilling the requirement. Please have a look on your data and judge yourself - which rows/units should be included. The code finds no such cases when

    Cj>Bi>Bj

    Even if we allow i<j (we do not - as per request) there is no match.

    I'd say points 1 and 3 are simple mathematics. We cannot change it

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

    Re: Loop through table and find max number of condition fulfillment

    Dear Kaper,

    Thx for detail explanation. Yea, i am agree with this points.

    Also I had checked initial data and understood that I misled myself and you Sorry...

    I assume that we can solve this task just to add a new condition:

    [Bx;Cx] --> i=1 (current row)
    [By;Cy] --> i=2 (next row)

    Main Condition for Looping:
    OR (Cy>Bx>By) OR (Cy>Cx>By)


    Is it possible to modify code in that way?

    Attached you can find new example for above mentioned case with good data

    With best regards,
    Igor
    Attached Files Attached Files

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Loop through table and find max number of condition fulfillment

    So in row 12 you have
    8290 8380 CR3
    and in row 20:
    8370 8460 CR2
    x = 12, y = 20, and "old reqirement" Cy>Bx>By
    is not met here.
    However, the "new requirement" (Cy>Cx>By) is met, so CR3 (row 12) will be listed after inserting this new requirement into the code:
    Please Login or Register  to view this content.
    Finding CR2 (row 20) would be against what you wrote before, about searching only below. But changing one loop (searching in j rows (j is always below i)
    Please Login or Register  to view this content.
    into searching in all above (from first to i-1) and below (form i+1 to last row)
    Please Login or Register  to view this content.
    would yield the result you found manually.
    Attached Files Attached Files

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

    Re: Loop through table and find max number of condition fulfillment

    Brilliant This is exactly what I need!

    Thank you very much for support

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Loop through table and find max number of condition fulfillment

    Ну, наконец это работает. Сначала у нас были проблемы с получением. Но в итоге это удалось.

    Большое спасибо за оценку

    (I hope I can still remember some Russian from my school times - not too often used nowadays )

+ 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. Loop through pivot table and return value based on condition
    By maym in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-12-2017, 10:35 PM
  2. How to find sort & sub total data from a table with some condition
    By tpsdas in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 01-06-2015, 12:08 PM
  3. Random number generation loop for a range of cells until condition is true!
    By gychoo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-30-2014, 11:21 PM
  4. [SOLVED] Find row number in For loop
    By aloha31 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-06-2013, 01:47 PM
  5. 1 of 3 choices to display in cell based on fulfillment criteria
    By dataguy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-08-2012, 01:39 PM
  6. [SOLVED] Count number of occurrences for a number of ranges from a find loop
    By ANDREWA in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-23-2012, 07:53 PM
  7. To find the number of occurrences based on the cell condition and retrieve the value
    By rsundarmail77 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-02-2011, 11:50 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