+ Reply to Thread
Results 1 to 11 of 11

Showing all combinations of a set of numbers

  1. #1
    Registered User
    Join Date
    12-22-2019
    Location
    UK
    MS-Off Ver
    2016
    Posts
    6

    Question Showing all combinations of a set of numbers

    I have 20 numbers for which I want to find all of the combinations of 8 of these numbers that sum to 26 is this possible to do?

    Edit 1:
    Below in the first column is the 20 numbers that I'm talking about.

    In the second column are the numbers that are associated to the numbers in the first column.

    In a nutshell my problem is that I want to find the largest sum of 8 of the numbers in the second column, however, the total for the numbers associated with them (in the first column) cannot exceed 26.

    6 163
    1 55
    1 46
    6 142
    2 55
    1 78
    2 99
    3 83
    1 39
    1 41
    2 32
    4 83
    4 92
    4 123
    2 55
    2 53
    6 100
    2 62
    2 52
    2 107
    Last edited by chris2005; 12-22-2019 at 01:57 PM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,460

    Re: Showing all combinations of a set of numbers

    Maybe attach a sample workbook. See the instructions at the top of the page.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Showing all combinations of a set of numbers

    Solver can be used if running it in a loop with VBA.

    This macro setup is based on a helper column (E) where all values at the start is set to 1 as well as clearing the range D3:D22 and the result range starting at range B28
    and going down to ???. Macro is set to dynamically find the last row with result.

    The macro then sets up the solver constraints, target cell C23 is set to be maximized. Then macro sets the constraints, range D3:D22 is set as binaries and range D23 is set to
    be equal to 8. Range B23 is set to be equal to 26 or less. Then macro runs solver and the result in range D3:D23 is filtered for values equal to 1 and 8 and the macro also finds the
    row number of the first value in the filtered range

    The filtered result is the copied to to B28 and the subsequent result is copied to the last value in the B column with an offset of 2.

    Macro now removes autofilter and using the previous found row number sets cells D and E to 0. Setting the E value to zero ensure that the number in this position can't
    be used by solver in the following runs as the sumproduct function also contains the helper column range (E3:E23).

    Finally the cell E23 is checked if it's 7 or less. If so macro run is stopped, if not solver does another run.

    In order to test this macro solver must be installed on the PC and a reference to solver must be set in VB.

    Developers tab, click on "Visual Basic" icon -> Tools -> References and click on box marked Solver.

    Alf
    Attached Files Attached Files
    Last edited by Alf; 12-24-2019 at 03:08 AM.

  4. #4
    Registered User
    Join Date
    12-22-2019
    Location
    UK
    MS-Off Ver
    2016
    Posts
    6

    Re: Showing all combinations of a set of numbers

    Thanks Alf! This is perfect for what I want.

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Showing all combinations of a set of numbers

    You are welcome

    Thanks for feedback and rep and a Merry Christmas to you.

    Alf

  6. #6
    Registered User
    Join Date
    12-22-2019
    Location
    UK
    MS-Off Ver
    2016
    Posts
    6

    Question Re: Showing all combinations of a set of numbers

    Quick question...
    I've added some extra rows and edited the visual basic code to adjust for the extra rows, however, when I re-run the macro cell C31 is not including the extra rows that I added.

    Do you know how this can be fixed?

    Merry Christmas

    Chris
    Attached Files Attached Files

  7. #7
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Showing all combinations of a set of numbers

    Will have a go at it tomorrow. Today the Swedes are celebrating Christmas and even if I'm a Norwegian I do have to join in in in the celebration. You (UK) do it tomorrow? That's "Boxing Day"?

    Alf

  8. #8
    Registered User
    Join Date
    12-22-2019
    Location
    UK
    MS-Off Ver
    2016
    Posts
    6

    Re: Showing all combinations of a set of numbers

    Thanks, sorry to bother you. I knew it was like that in Poland but wasn't sure about Sweeden/ Norway.

    Enjoy the rest of your day!

    Chris

  9. #9
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Showing all combinations of a set of numbers

    Thanks, sorry to bother you.
    No problem Chris. After yesterdays celebration it feels good to exercise the brain a bit today. Looking at your uploaded file I found a small problem in the formula in
    cell C31 i.e. "=SUMPRODUCT(B3:B22;D3:D22;E3:E22)" but because of the extended range it should be "=SUMPRODUCT(B3:B28;D3:D28;E3:E28"). The ";" is the Scandinavian delimiter
    for excel functions.

    The C31 problem you mentioned well in my uploaded file I defined the constraint to cell C31 and constraint value to E31 but in the macro where you find the solver setting
    I've set B23 to be equal or less than E25 but in the uploaded file I've defined C25 as the constraint and E25 as the max value but both B23 and C25 contains the same formula
    so they are equal but yes it can be puzzling.

    Stepping through the macro in your uploaded file I did find another much more serious problem. Macro added 13 repetitive results!!!! at the end.

    So I modified the formula in cell D29 from "=SUM(D3:D28)" to "=SUMPRODUCT(D3:D28;E3:E28)" and this fixed the repetitive result as well as changing the "If" command for
    the jump (skipper) command to "<=8" as it seemed that the original "<=7" command did not work well.

    If I compare the result using 7 or 8 for the jump command there are no difference in results that meets the criteria but using 7 I get an false (not meeting criteria) result pasted to
    range B214 and a decimal number in cell in B23, but using 8 I only get a 0 value in cell D26 and these tings worry me as I don't know why.

    Will probably do a modified macro during the week that places the results in columns next to the original column of values for easily comparison of the different result.

    Alf
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    12-22-2019
    Location
    UK
    MS-Off Ver
    2016
    Posts
    6

    Question Re: Showing all combinations of a set of numbers

    Glad you had a good time yesterday, as you correctly said yesterday, today is the main day of Christmas celebration for us, so spending today with my family.

    Yes I thought there was a problem with the macro as when I ran it there were no numbers in the "bin grd" column and the helper column was showing too many 1s. So thanks for fixing that!

    If in the future I need to add some extra rows is it just a case of making sure that I correctly update the cell references in the macro, as well as, the formulae in rows 30, 32 and 33?

    Chris
    Last edited by chris2005; 12-25-2019 at 10:20 AM.

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Showing all combinations of a set of numbers

    I correctly update the cell references in the macro, as well as, the formulae in rows 30, 32 and 33?
    Yes that's correct. So now enjoy the Christmas season with your family

    And I've uploaded a modified file with two macros one presents the result vertically (as before) and the other macro places the result horizontally. As I was not sure if
    all the numbers was used at least once I did a check with the countif function in column BZ and they are.

    Alf
    Attached Files Attached Files

+ 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. [SOLVED] To find all combinations of numbers from list of numbers that adds upto a specified number
    By Vivek2705 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-07-2019, 06:33 AM
  2. getting all possible set combinations of a set of numbers
    By juares castro in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-12-2015, 04:04 PM
  3. please help!!ALL COMBINATIONS REQUIRED FOR 5-DIGIT NUMBERs using numbers 0-9
    By natasha mirembe in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-08-2014, 01:43 PM
  4. [SOLVED] Different Combinations of 24 Numbers
    By danedw in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-20-2013, 07:03 AM
  5. Replies: 0
    Last Post: 08-23-2012, 12:36 PM
  6. Replies: 1
    Last Post: 04-15-2012, 09:23 AM
  7. Possible Combinations Of A Given Set Of Numbers??
    By Jennylyn1 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-01-2007, 06:24 PM

Tags for this Thread

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