+ Reply to Thread
Results 1 to 22 of 22

List all combinations

  1. #1
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    List all combinations

    I'm new to Excel and I want to know if there is a function that will list combinations of numbers.

    I will simplify what I mean.

    Suppose I sell flowers.
    Suppose I have 20 different kinds of flowers in my store.
    The flowers are Flowers A, B, C, D, etc... until Flower T.

    Now, suppose I want to sell my flowers in different combinations of 4 flowers, for example:

    Flower A, Flower B, Flower C and Flower D
    Flower A, Flower C, Flower D and Flower E
    Flower B, Flower C, Flower D and Flower E
    Flower P, Flower R, Flower S and Flower T
    etc...

    Is there an Excel function that could list all the options for me?

    Thanks!

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: List all combinations

    You may say it with flowers, but we make it with a macro.

    See the attached
    Attached Files Attached Files
    Gary's Student

  3. #3
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Jakob, that was pretty cool! Wow! Thanks! And so quick! You're gifted!

    Ok, now I'm trying to understand the code so I can modify it as I make changes.


    Here's your code:

    Sub Flowers()
    fl = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T"
    flo = Split(fl, ",")
    MsgBox LBound(flo) & vbCrLf & UBound(flo)
    N = 1
    For i = 0 To 19
    a = flo(i)
    For j = i + 1 To 19
    b = flo(j)
    For k = j + 1 To 19
    c = flo(k)
    For l = k + 1 To 19
    d = flo(l)
    Cells(N, 1) = a
    Cells(N, 2) = b
    Cells(N, 3) = c
    Cells(N, 4) = d
    N = N + 1
    Next
    Next
    Next
    Next
    End Sub
    What would I have to do to the code to list 9 flower combinations instead of 4 flower combinations?

    Would I have to add the lines below to your code?

    e = flo(m)
    f = flo(n)
    g = flo(o)
    h = flo(p)
    i = flo(q)

    Cells(N, 5) = a
    Cells(N, 6) = b
    Cells(N, 7) = c
    Cells(N, 8) = d

    Next
    Next
    Next

    Thanks!

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: List all combinations

    You would need to add more nested For loops. It is easy to do but you will soon run out of rows to store the stuff!

  5. #5
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Ok, then, I hate to ask, but, what if I wanted the list displayed from right to left, accross columns, rather than from top to bottom, accross rows?

    Go from this result:

    1 2 3 4
    1 2 3 5
    1 2 3 6
    1 2 3 7

    To this one:

    1 1 1 1
    2 2 2 2
    3 3 3 3
    4 5 6 7

    Thanks!

  6. #6
    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: List all combinations

    Thare's a workbook that will do this at https://www.box.com/s/b9b9fc06beb63b9562f9
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Quote Originally Posted by shg View Post
    Thare's a workbook that will do this at https://www.box.com/s/b9b9fc06beb63b9562f9
    Thanks, shg... I looked at the workbook and though I think it does what I asked for - listing all the combinations - but what I like about Jakobshavn's solution (above) is that I get the results with each each "Item" - or "Flower" in my example- in a particular 4-Item group - in separate cells, which means I can add other attributes to these Items. When I get a result in one cell or line I can't manipualte the data further. Do I make sense?

    I love Jakobshavn's solution, I'm trying to see how I can make it scaleable, adding types of "Flowers" and getting larger groups, up to 9 at most. Given a choice, I'd also like the results posted across columns rather than vertically, across rows...

  8. #8
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Quote Originally Posted by Jakobshavn View Post
    You would need to add more nested For loops. It is easy to do but you will soon run out of rows to store the stuff!
    How do I add these nested For loops?

    For example, do I just add the line:

    Please Login or Register  to view this content.

    Or do I also have to add:

    Please Login or Register  to view this content.
    I'm trying to figure out how to scale this up, 'cause I'd like to scale up to combinations of 5, 6, 7, 8 amd 9 flowers eventually....

  9. #9
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Jakob, I'm really impressed with this... How can I decipher the syntax to the language you're using? I'm impressed! Can you explain what each line does?... Thanks!

  10. #10
    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: List all combinations

    You can do text to columns with the result:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    How do I do text to columns? Export the data? Copy to a text file? I like the output but how do I do it? I feel ignorant....

  12. #12
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: List all combinations

    Last edited by martindwilson; 02-10-2013 at 05:58 PM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  13. #13
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    shg, other than how I do the conversion, I was playing around with the spreadsheet and the results I get are in the inverse order I'd like them. So using A,B,C,D and 4-item groups I get:


    D C B A
    E C B A
    E D B A
    E D C A etc...

    As oppsoed to:

    A B C D
    A B C E
    A B C F
    A B C G

    I know I can sort them later, but why does it prsent them that way?...

  14. #14
    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: List all combinations

    Because it puts them in lexical order. If you replace the flower names with element numbers {0,1,2,3,4,5,...,N}, it lists all the combinations of 4 choose 4 (there's only 1):

    3 2 1 0

    ... then the remaining combinations of 5 choose 4:

    4 2 1 0
    4 3 1 0
    4 3 2 0
    4 3 2 1

    ... then the remaining combinations of 6 choose 4:

    5 2 1 0
    5 3 1 0
    5 3 2 0
    5 3 2 1
    5 4 1 0
    5 4 2 0
    5 4 2 1
    5 4 3 0
    5 4 3 1
    5 4 3 2

    ... and so forth.

    Notice that those are sorted ascending.

  15. #15
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Quote Originally Posted by martindwilson View Post
    Thanks, that was helpful!

  16. #16
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Quote Originally Posted by shg View Post
    Because it puts them in lexical order. ... Notice that those are sorted ascending.
    Got it... I understand... Ok, now with the results presented that way, is there an autosort function which would put them in the order I want them - ABCD, ABCE, ABCF... ? ... Thanks, shg, mighty prompt of you!

  17. #17
    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: List all combinations

    No. After you do text to columns, you'd need to sort each row left to right, and then everything top to bottom, if you want them in that order.
    Last edited by shg; 02-10-2013 at 07:11 PM.

  18. #18
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Quote Originally Posted by shg View Post
    ... you'd need to sort each row left to right, and then everything top to bottom, if you want them in that order.
    Thank you so much, shg!...

  19. #19
    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: List all combinations

    You're welcome, good luck.

  20. #20
    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: List all combinations

    I added a button that reorders the list. Same link.

  21. #21
    Registered User
    Join Date
    02-10-2013
    Location
    PR
    MS-Off Ver
    Excel 2003
    Posts
    93

    Re: List all combinations

    Quote Originally Posted by shg View Post
    I added a button that reorders the list...
    Wow, now that is impressive. It's beautiful! Crazy nice. Downloaded it and am enjoying the hell out of it. Thanks!

  22. #22
    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: List all combinations

    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)

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