+ Reply to Thread
Results 1 to 21 of 21

vba code for Combinations

  1. #1
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    vba code for Combinations

    I should be able to get my head round this but have a complete mental block. Im trying to write an algorithim and then a vba user defined function to solve the following problem:

    I have N (max 32) display panels and each panel can have any one of R (say 4) layout configurations. I want to write a code which will output an array of N columns with each row populated with the Layout number (I've used letters for clarity only):

    For example a 3 panel situation would output

    AAA
    AAB
    AAC
    AAD
    ABB
    ABC
    ABD
    ACC
    ACD
    ADD
    BBB
    BBC
    BBD
    BCC
    BCD
    BDD
    CCC
    CCD
    CDD
    DDD

    I know the maths to work out the number of non repetetive combinations - its the logic of generating the aray table which has defeated me.

    Any help would be gratefully received.

    Thanks in anticipation

  2. #2
    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: vba code for Combinations

    Welcome to the board. For a formula-based solution,

    Row\Col
    A
    B
    C
    D
    2
    A A A A2: =CHAR(65 + MOD(INT(4 * (ROWS($C$2:Me) - 1) / 4 ^ COLUMNS($C$2:Me)), 4))
    3
    A A B
    4
    A A C
    5
    A A D
    6
    A B A
    7
    A B B
    8
    A B C
    9
    A B D
    10
    A C A
    11
    A C B
    12
    A C C
    13
    A C D
    14
    A D A
    15
    A D B
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    Re: vba code for Combinations

    Thanks Guru for the very quick response. I seem to get a Name error when entering this formula in A2??

    I suspect the formula based approach may work well for small values of N but I do need a vba solution in order to develop my project further.

    Thanks again

  4. #4
    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: vba code for Combinations

    Sorry, it's a standard name for me.

    In A2,

    =CHAR(65 + MOD(INT(4 * (ROWS($C$2:A2) - 1) / 4 ^ COLUMNS($C$2:A2)), 4))

  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: vba code for Combinations

    Via VBA,

    Row\Col
    A
    B
    C
    D
    E
    1
    States
    ABCDE ABCD
    2
    Start
    AAA B2: Input AAAA D2: Input
    3
    AAB B3: =DecToBij(BijToDec(B2, B$1) + 1, B$1) AAAB D3: =DecToBij(BijToDec(D2, D$1) + 1, D$1)
    4
    AAC AAAC
    5
    AAD AAAD
    6
    AAE AABA
    7
    ABA AABB
    8
    ABB AABC
    9
    ABC AABD
    10
    ABD AACA
    11
    ABE AACB
    12
    ACA AACC
    13
    ACB AACD
    14
    ACC AADA
    15
    ACD AADB
    16
    ACE AADC
    17
    ADA AADD


    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code for Combinations

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  7. #7
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    Re: vba code for Combinations

    shg, Many thanks for the revision and vba code. I'll work on this but it's certainly given me the kick start I needed. Regards

  8. #8
    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: vba code for Combinations

    You're welcome.

  9. #9
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    Re: vba code for Combinations

    mehmetcik, I was sure I had replied to your post yesterday but clearly not! Thanks very much indeed for this code. I have run it but I get a Run-Time 1004 error Application defined or Object defined error. So I changed the 32 columns to 4 and ran it again. This time the output ran to 625 lines and a sample of the first few is as below:

    On running Debug there appears to be a problem at the line "Range(Cells(Row, 1), Cells(Row, 32)).Value = MyArray2". Should there be a dimension variant for MyArray2?. Also in the line above you have "Display = Display+1 but I cannot see where that variable appears elsewhere in the routine.

    I would be very grateful if you could please have a look again and point me in the right direction. Your help is much appreciated

    0 0 0 0
    0 0 0 A
    0 0 0 B
    0 0 0 C
    0 0 0 D
    0 0 A 0
    0 0 A A
    0 0 A B
    0 0 A C
    0 0 A D
    0 0 B 0
    0 0 B A
    0 0 B B
    0 0 B C
    0 0 B D
    0 0 C 0
    0 0 C A
    0 0 C B
    0 0 C C
    0 0 C D
    0 0 D 0
    0 0 D A
    0 0 D B
    0 0 D C
    0 0 D D

  10. #10
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code for Combinations

    I just ran the code without modification and stopped it at line 365715

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D C B 0 0 C B D

    I will let it run to its conclusion.

  11. #11
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code for Combinations

    oops, it crashes at row 1048576,I didn't think you would need that many rows. I can set it to move across 40 columns at 1048476 and continue there. BRB

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B C B 0 B C C 0 0



    revised code:

    Please Login or Register  to view this content.
    Last edited by mehmetcik; 03-20-2016 at 08:14 AM.

  12. #12
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code for Combinations

    It crashed again, must be running out of memory,

    I have modified MyArray2 to save memory and trying again.

    I only have a small laptop.

    It might work better on an office PC

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    Re: vba code for Combinations

    Thanks for your continuing work. I hope we are not at cross purposes here. I was anticipating the output for a 4 col ABCD arrangement to be:
    AAAA
    AAAB
    AAAC
    AAAD
    AABB
    AABC
    AABD
    AACC
    AACD
    AADD
    ABBB
    etc.
    This is for all the combinations without repetition. The maths to work out what the number of non repeating combinations is (N+R-1)!/R!(N-1)! So for R=32 and N=4 there are only 6,545 combinations and for R=4 and N=4 only 35

    R=32 is what I will eventually need and I was going to adapt your code into a user defined function into which I could pass variable values for R & N. Does the above make it any easier?? Cheers DJH

  14. #14
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code for Combinations

    It crashed at row 1048477 hmmmm.


    I think a separate pages might do the trick.

    Please Login or Register  to view this content.
    B C B 0 B B D 0 A
    Last edited by mehmetcik; 03-20-2016 at 08:55 AM.

  15. #15
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    Re: vba code for Combinations

    The logic I was trying to work out an algorithm for was that (taking the easier 4 column example), we start with AAAA and then for each subsequent row cols 1 to 3 stay on A and col 4 increments +1. When col 4 =4 row 3 of col 5 would increment by 1 (i.e. become B) and col 4 of row 5 would start at B (i.e. the same as col 3 ) etc. Sorry if that's rather long hand bit it's where I got in a mess trying to implement! DJH

  16. #16
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code for Combinations

    I would have expected your output to be:-

    A A A A
    A A A B
    A A A C
    A A A D
    A A B A
    A A B B
    A A B C
    A A B D
    A A C A
    A A C B
    A A C C
    A A C D
    A A D A
    A A D B
    A A D C
    A A D D
    A B A A
    A B A B
    A B A C
    A B A D
    A B B A
    A B B B
    A B B C
    A B B D
    A B C A
    A B C B
    A B C C
    A B C D
    A B D A
    A B D B
    A B D C
    A B D D
    A C A A
    A C A B
    A C A C
    A C A D
    A C B A
    A C B B
    A C B C
    A C B D
    A C C A
    A C C B
    A C C C
    A C C D
    A C D A
    A C D B
    A C D C
    A C D D
    A D A A
    A D A B
    A D A C
    A D A D
    A D B A
    A D B B
    A D B C
    A D B D
    A D C A
    A D C B
    A D C C
    A D C D
    A D D A
    A D D B
    A D D C
    A D D D
    B A A A
    B A A B
    B A A C
    B A A D
    B A B A
    B A B B
    B A B C
    B A B D
    B A C A
    B A C B
    B A C C
    B A C D
    B A D A
    B A D B
    B A D C
    B A D D
    B B A A
    B B A B
    B B A C
    B B A D
    B B B A
    B B B B
    B B B C
    B B B D
    B B C A
    B B C B
    B B C C
    B B C D
    B B D A
    B B D B
    B B D C
    B B D D
    B C A A
    B C A B
    B C A C
    B C A D
    B C B A
    B C B B
    B C B C
    B C B D
    B C C A
    B C C B
    B C C C
    B C C D
    B C D A
    B C D B
    B C D C
    B C D D
    B D A A
    B D A B
    B D A C
    B D A D
    B D B A
    B D B B
    B D B C
    B D B D
    B D C A
    B D C B
    B D C C
    B D C D
    B D D A
    B D D B
    B D D C
    B D D D
    C A A A
    C A A B
    C A A C
    C A A D
    C A B A
    C A B B
    C A B C
    C A B D
    C A C A
    C A C B
    C A C C
    C A C D
    C A D A
    C A D B
    C A D C
    C A D D
    C B A A
    C B A B
    C B A C
    C B A D
    C B B A
    C B B B
    C B B C
    C B B D
    C B C A
    C B C B
    C B C C
    C B C D
    C B D A
    C B D B
    C B D C
    C B D D
    C C A A
    C C A B
    C C A C
    C C A D
    C C B A
    C C B B
    C C B C
    C C B D
    C C C A
    C C C B
    C C C C
    C C C D
    C C D A
    C C D B
    C C D C
    C C D D
    C D A A
    C D A B
    C D A C
    C D A D
    C D B A
    C D B B
    C D B C
    C D B D
    C D C A
    C D C B
    C D C C
    C D C D
    C D D A
    C D D B
    C D D C
    C D D D
    D A A A
    D A A B
    D A A C
    D A A D
    D A B A
    D A B B
    D A B C
    D A B D
    D A C A
    D A C B
    D A C C
    D A C D
    D A D A
    D A D B
    D A D C
    D A D D
    D B A A
    D B A B
    D B A C
    D B A D
    D B B A
    D B B B
    D B B C
    D B B D
    D B C A
    D B C B
    D B C C
    D B C D
    D B D A
    D B D B
    D B D C
    D B D D
    D C A A
    D C A B
    D C A C
    D C A D
    D C B A
    D C B B
    D C B C
    D C B D
    D C C A
    D C C B
    D C C C
    D C C D
    D C D A
    D C D B
    D C D C
    D C D D
    D D A A
    D D A B
    D D A C
    D D A D
    D D B A
    D D B B
    D D B C
    D D B D
    D D C A
    D D C B
    D D C C
    D D C D
    D D D A
    D D D B
    D D D C
    D D D D


    Created using this code:-
    Please Login or Register  to view this content.

  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: vba code for Combinations

    Ah -- you're looking for draw with replacement.

    The workbook at https://app.box.com/s/cj3cjtij207amp5q583161f97g6y8n8s does that.

  18. #18
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    Re: vba code for Combinations

    My thanks once again. This certainly works and I see you are expecting the required output to be all permutations of ABCD. However, order is not important so if you take line 2 at AAAB then line 5 is AABA which is the same combination. Hence my basic algorithm that when Col(x) contains D [or 4 if numeric] then on the next row Col (x-1) = previous row Col(x)-1 and this next row Col(x) starts at Col(x-1) on the same row.

    So If say Row = 5 then
    Col(1) =1
    Col(2) =1
    Col(3) = Col(3)+1 from Row 4
    Col(4) now = Col(3)

    and Col(4) continues to be incremented until it is 4 again and so on

    Sorry again to be taking so much of your time but it's really appreciated. DJH

  19. #19
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    Re: vba code for Combinations

    Thanks shg, it certainly gives the output but the options and hence the code is perhaps a little too complex for my needs. In addition I do need each element of each combination to be in its own column. I guess I could intercept the code to do that. I'm sure mehmetcik is so close with his last version.

  20. #20
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: vba code for Combinations

    So if I give an A a value of 1
    and a B a value of 10
    and a C a value of 100
    and a D a value of 1000

    and calculate a value of each output in columns A to B in column F

    I can then use countif to find the first occurrence of each result
    I then delete all duplicates.

    Looking forward, it would be quite simple to build in the countif during the first part of the macro, to delete duplicates as they are created.

    A A A A
    A A A B
    A A A C
    A A A D
    A A B B
    A A B C
    A A B D
    A A C C
    A A C D
    A A D D
    A B B B
    A B B C
    A B B D
    A B C C
    A B C D
    A B D D
    A C C C
    A C C D
    A C D D
    A D D D
    B B B B
    B B B C
    B B B D
    B B C C
    B B C D
    B B D D
    B C C C
    B C C D
    B C D D
    B D D D
    C C C C
    C C C D
    C C D D
    C D D D
    D D D D


    This is the code I used:-


    Please Login or Register  to view this content.
    Last edited by mehmetcik; 03-20-2016 at 08:30 PM.

  21. #21
    Registered User
    Join Date
    03-18-2016
    Location
    Flitwick, England
    MS-Off Ver
    2010
    Posts
    10

    Post Re: vba code for Combinations

    Once again my thanks. In the meantime I have been trying to solve the algorithm for mysef - the only way to learn!
    Everything has worked as per the code below. However, there are a few things not going quite according to plan which it would be helpful if you could plesase look into. Firstly when the macro has run I seem to have "lost" all the menu ribbons. Sometimes the sheet has lost is auto calcualtion.
    I'm tring to pass the numeric value from my numeric TempArray() as a character to my RealArray(x,y) but I'm getting a subscript error
    Finally I thought I would be able to grab the values in cells from the worksheet and assign them to my vba variables. This works OK for anything other than a cell which contains a calculated value.
    I have inserted the code into a new module - is that the correct way?
    If you try and run the code you'll se what I mean. You'll need to put values in cells A2, B3 and B4 Suggest 56, 5 and 4 respectively.
    I'm sure that once these few isues are ironed out I'll be able to continue unaided with my project! Thanks again. DJH

    Please Login or Register  to view this content.

+ 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] Combinations without repetition - VBA code partially working
    By Arthfael in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-14-2015, 05:48 AM
  2. Looking for permutation code for alphahnumeric combinations
    By nextonmags in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2014, 12:30 AM
  3. [SOLVED] VBA code to calculate Average value of number combinations
    By Chubster in forum Excel General
    Replies: 2
    Last Post: 03-28-2012, 08:50 PM
  4. Modify Combinations Code
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-02-2010, 11:03 AM
  5. Macro/Code to get all possible combinations
    By Jordans121 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-15-2010, 03:56 AM
  6. Subscript out of range error using all possible combinations code
    By alisalamiii in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-05-2010, 09:57 AM
  7. Code For Combinations
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-11-2005, 12:28 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