+ Reply to Thread
Results 1 to 7 of 7

Random counting of cells to meet a given number

  1. #1
    Registered User
    Join Date
    01-20-2012
    Location
    Yok, England
    MS-Off Ver
    Office 365
    Posts
    85

    Random counting of cells to meet a given number

    I'm looking for a formula which will pick a number of numbers and add then and try and get as close as possible to a given number. The number will not be met exactly but must not exceed the number, but I want excel to select the best possible numbers to ensure its as close as possible.

    eg:
    To get as close to 1000 as possible. (not over, must be under)

    Range of numbers: 90, 300, 600, 500, 50.

    We know that by picking 90+300+600 = 990 (10 remaining). Is better than 300+600+50 = 950 (50 remaining).

    The data numbers I have is about 300 numbers not 5 numbers in the example above, is there a way when the formula selects the most appropriate numbers to total up, it will show which numbers have been selected?

    Thanks everyone.
    Last edited by Dougie12.; 09-20-2017 at 03:07 AM. Reason: whoops, maths!

  2. #2
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Random counting of cells to meet a given number

    smth like in the file
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    01-20-2012
    Location
    Yok, England
    MS-Off Ver
    Office 365
    Posts
    85

    Re: Random counting of cells to meet a given number

    Wow, very impressive thank you for the time you've spent on this.

    Although the problem is, I'm not sure how you've done this, could you explain why Macros have been used and what the three buttons are?

    Attached is the file which shows the numbers I'm trying to achieve (2400,3000,3600,4200 & 4800) and the numbers when added to equal any of those scenarios are attached.

    Are you able to re run your scenario and explain how it works?

    thanks again.
    Attached Files Attached Files

  4. #4
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Random counting of cells to meet a given number

    I did not make those macros. if you are going to proceed with them, try to understand VBA

  5. #5
    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,613

    Re: Random counting of cells to meet a given number

    The problem you are trying to solve is one of typical programming tasks - bacpack/knapsack problem.
    while description sounds simple, the actual computing power/time to find exact solution is enormous (already for mid-size datasets).
    Reasonable description can be found on wiki: https://en.wikipedia.org/wiki/Knapsack_problem

    The file posted by tim201110 has 3 algoritms of searching quite-good (but not necesserily best) solutions implemented
    File (or parts of code) come from other (russian) forums - see the code itself for comments - they are written in cyrlic, so unreadable in plain english VBE,unless you change font into one with cyrlic alphabet - but ... can you read it? I do :-) ... but links to threads remained intact, like:
    longdynamic: http://forum.sources.ru/index.php?showtopic=204375
    slan: http://www.planetaexcel.ru/forum/ind...ID=8&TID=31231
    etc.

    To study these /and other algorithms with code commented in English, I'd suggest searching this forum (after short playing with our search engine i found some posts by shg, MrShorty, Alf and others...)
    Last edited by Kaper; 09-21-2017 at 05:14 AM. Reason: note on cyrlic alphabet
    Best Regards,

    Kaper

  6. #6
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Random counting of cells to meet a given number

    Finding All Combinations That Equal Given Sum
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    01-20-2012
    Location
    Yok, England
    MS-Off Ver
    Office 365
    Posts
    85

    Re: Random counting of cells to meet a given number

    Thank you Tim, this is perfect. Much appreciated.

+ 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. Counting Consecutive Cells that meet Criteria
    By Omega Point in forum Excel General
    Replies: 3
    Last Post: 03-07-2023, 04:44 AM
  2. [SOLVED] Counting cells that do not meet criteria
    By Spicey_888 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-20-2014, 02:37 AM
  3. Replies: 5
    Last Post: 07-22-2013, 05:56 PM
  4. [SOLVED] counting number of cells which meet a certain criteria
    By Laurab in forum Excel General
    Replies: 8
    Last Post: 06-19-2012, 08:58 AM
  5. Counting cells that meet 2 differnet criteria
    By USChad in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-07-2005, 03:05 PM
  6. counting cells that meet certain criteria
    By StJohn in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-14-2005, 04:25 PM
  7. [SOLVED] Counting Values that meet another cells criteria
    By Jess in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-07-2005, 10:06 PM

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