+ Reply to Thread
Results 1 to 7 of 7

How to generate a list of all possible combinations of items

  1. #1
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    How to generate a list of all possible combinations of items

    Hi

    I'm looking for VBA code to help create a table similar to the one below that shows the list of all possible combinations of N objects taken in groups of P.
    For example, this would be partial results of 6 objects (A,B,C,D, E F) taken 3 at a time.

    Please Login or Register  to view this content.
    Hopefully, someone may be able to help.
    - Stu

  2. #2
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: How to generate a list of all possible combinations of items

    Try this

    Please Login or Register  to view this content.
    Happy with my advice? Click on the * reputation button below

  3. #3
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: How to generate a list of all possible combinations of items

    Thank you Crooza for your sample macro. It got me started towards a solution.
    After DIM-ing all the variables (I use 'Option Explicit'), it produced results for 3 groups of 3 items taken from 6.
    Here's my modified code...
    Please Login or Register  to view this content.
    Next I wanted to allow a variable number of items rather than 4.
    Experimenting a bit gave this code for 4 items out of N.
    ( N is entered in cell B1 and a button starts the macro; This makes it more flexible.)

    Please Login or Register  to view this content.
    Extrapolating from this, I can see that making groups of 5 would require 5 counters and FOR loops, groups of 6 would need 6, and so on.

    My next task is to generalize this to have one routine to make the groups of size K chosen from N items.
    For my purposes, going up to groups of 10 chosen from 15 items would be sufficient.
    When (and if) that's working, I will post it also.

    -Stu
    Last edited by StuCram; 04-29-2015 at 10:38 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: How to generate a list of all possible combinations of items

    OK, I've now made a demo to generate all possible groups of size K from a set of N items.

    The Excel file with the VBA macro program is attached for anyone curious.
    I used a SELECT/CASE structure to execute the appropriate group of FOR loops.
    I'm pleased to at how quickly the macro runs, even with several nested FOR loops

    - Stu

    combinations.xlsm

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: How to generate a list of all possible combinations of items

    Here's an alternative that works for any combination of n and m without nested loops:

    Please Login or Register  to view this content.
    Last edited by shg; 04-29-2015 at 12:28 PM.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: How to generate a list of all possible combinations of items

    Thank you SHG for your alternate solution.
    I'm always intrigued that there frequently is more than one method to attack a problem.
    I like how your solution generates the item#s within the group.
    That however is not what I needed; I need the 'x' to appear in the appropriate columns as shown in my attachment.
    It should not be difficult, if I wish, to adapt your method to do that.
    The beauty of your solution of course is it's generality for the # of items and the group size.

    Thanks, again.
    - Stu

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: How to generate a list of all possible combinations of items

    You're welcome.

+ 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. read array of values to generate a list of combinations
    By envy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-17-2014, 12:14 PM
  2. List of 15 elements, need to generate all 6 element combinations.
    By j4c0b5m17h in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-22-2013, 02:11 PM
  3. Replies: 4
    Last Post: 08-18-2013, 06:38 AM
  4. [SOLVED] Run-time error 6: Overflow - Trying to generate list of combinations
    By AdamParker in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-22-2013, 10:54 AM
  5. Generate list of possible combinations
    By Pat Parker in forum Excel General
    Replies: 2
    Last Post: 03-14-2010, 03:34 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