+ Reply to Thread
Results 1 to 3 of 3

Macros to generate all possible combinations

  1. #1
    Registered User
    Join Date
    09-01-2012
    Location
    Nigeria
    MS-Off Ver
    Excel 2007
    Posts
    2

    Post Macros to generate all possible combinations

    Hi Guys
    I have a table of 5 columns and 970 rows.Table containing only numbers (lottery draws)
    Suppose i want a macro that will generate all possible combination of 5C2 and 5C3 and
    i want the result of 5C2 to appear in two columns and that of 5C3 in three columns.
    here is an example to illustrate

    A B C D E
    1 2 3 4 5

    Result for 5C3 should look like this
    F G H
    1 2 3
    1 2 4
    1 2 5
    1 3 4
    1 3 5
    1 4 5
    2 3 4
    2 3 5
    2 4 5
    3 4 5
    Anyone with an idea.
    here is my file
    Attached Files Attached Files

  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: Macros to generate all possible combinations

    See the workbook at https://www.box.com/s/b9b9fc06beb63b9562f9
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-01-2012
    Location
    Nigeria
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Macros to generate all possible combinations

    A B C D E
    1 24 40 52 82
    7 12 18 32 49
    14 18 73 74 83
    15 18 44 59 79
    27 52 57 63 72
    12 23 33 52 81
    3 6 41 78 83
    9 20 33 57 58
    22 23 45 57 78

    F G H
    1 24 40
    1 24 52
    1 24 82
    1 40 52
    1 40 82
    1 52 82
    24 40 52
    24 40 82
    24 52 82
    40 52 82
    7 12 18
    7 12 32
    etc.
    The later table above is what the desired result should look
    like for that of 5C3.here is the macros have been struggling with
    Sub combo()
    '
    ' Combo Macro

    RowOffset = 1
    Col1 = 1
    Col2 = 2
    Col3 = 3
    Col4 = 4
    Col5 = 5
    Col7 = 7
    Col8 = 8
    Col9 = 9
    CurRow = 3

    For I = A1 To C3
    For J = B1 To D3
    For K = C1 To E3
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col1).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col2).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col3).Value
    CurRow = CurRow + 1
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col1).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col2).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col4).Value
    CurRow = CurRow + 2
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col1).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col2).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col5).Value
    CurRow = CurRow + 3
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col1).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col3).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col4).Value
    CurRow = CurRow + 4
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col1).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col3).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col5).Value
    CurRow = CurRow + 5
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col1).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col4).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col5).Value
    CurRow = CurRow + 6
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col2).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col3).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col4).Value
    CurRow = CurRow + 7
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col2).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col3).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col5).Value
    CurRow = CurRow + 8
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col2).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col4).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col5).Value
    CurRow = CurRow + 9
    Cells(CurRow, Col7).Value = Cells(RowOffset + I, Col3).Value
    Cells(CurRow, Col8).Value = Cells(RowOffset + J, Col4).Value
    Cells(CurRow, Col9).Value = Cells(RowOffset + K, Col5).Value
    CurRow = CurRow + 10
    Next K
    Next J
    Next I
    End Sub
    The problem with this code is that it only produce the combo
    for the first row with a lot of space between result and it also
    failed to produce the combo for subsequent row.Here is an attachment
    to clarify things.
    Attached Files Attached Files
    Last edited by Nnamdi; 12-25-2012 at 08:13 AM. Reason: need to attach a file

+ 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