Consulting

Results 1 to 5 of 5

Thread: Permutation List Code

  1. #1

    Permutation List Code

    Hi,

    I would like to know how to populate 27 rows with the 27 permutations of a,b,c (in different columns of each row). So it would look something like
    a,a,a
    b,a,a
    b,b,a
    b,b,b
    a,b,b
    c,a,a
    ... to 27th row

    This would probably require a triple nested loop. This code in javascript does the trick, but I am unable to translate something like it into vba. Here's the javascript code if it helps:

    var set = ['a', 'b', 'c'];
    var result = [];

    var elt = ";
    for (var i = 0 < set.length; i++) {
    for (var j = 0; j < set.length; j++) {
    for (var k = 0; k< set.length; k++) {
    elt = set[i] + set[j] + set[k];
    result.push(elt);
    }
    }
    }

    Thanks for your help!

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Not the most efficient, but close to your example

    Option Explicit
    Option Base 1
    Sub ABC()
        Dim i1 As Long, i2 As Long, i3 As Long, iOut As Long
        Dim aABC As Variant
        
        aABC = Array("a", "b", "c")
        
        iOut = 1
        
        Application.ScreenUpdating = False
        
        For i1 = LBound(aABC) To UBound(aABC)
            For i2 = LBound(aABC) To UBound(aABC)
                For i3 = LBound(aABC) To UBound(aABC)
                    ActiveSheet.Cells(iOut, 1).Value = aABC(i1)
                    ActiveSheet.Cells(iOut, 2).Value = aABC(i2)
                    ActiveSheet.Cells(iOut, 3).Value = aABC(i3)
                    iOut = iOut + 1
                Next i3
            Next i2
        Next i1
    
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Hi

    Just for fun, a one-liner (specific to this problem):

    Sub Permut()
    
    Range("C2").Resize(27, 3).Value = Application.Index(Array("a", "b", "c"), Evaluate("if(row(1:27),mod(int((row(1:27)-1)/{9,3,1}),3)+1)"))
    End Sub

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    another one:
    Sub M_snb()
        Cells(1, 5).Resize(27) = [index(char(97+int((row(1:27)-1)/9)) & char(97+int(mod((row(1:27)-1),9)/3))& char(97+mod(row(1:27)-1,3)),)]
    End Sub
    or even:

    Sub Permuta()
        Range("C2").Resize(27, 3).Value = [Index(char(96+mod(int((row(1:27)-1)/{9,3,1}),3)+1),)]
    End Sub
    Last edited by snb; 07-02-2014 at 09:05 AM.

  5. #5
    Thanks everyone, I really appreciate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •