+ Reply to Thread
Results 1 to 3 of 3

Excel Permutations

  1. #1
    Registered User
    Join Date
    07-02-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    2

    Excel Permutations

    Hello,
    I am currently trying to solve a problem using a brute force method but because I last worked with excel a while ago, my logic/algorithm creation is very rusty.

    Here is the problem.

    I need an algorithm to try all possible permutations for 12 numbers where each number can only be between 1 and 3 so that is only 531,441 possible permutations (This will at most increase to 4 which is still only 16 million permutations).
    It needs to put in the permutation in the 12 cells end whenever a constraint cell is equal to zero. (The cell is 0 only when all constraints are not broken).

    I have no idea how to do this. I would think it would involve a For and Do While loop but for the life of me I can't figure it out.
    Help is greatly appreciated.

    ChrisSS2ChrisTodorovSolverReworked2.xlsm

    I added the excel file I am currently working on, the problem is on Output 5 sheet.
    Last edited by jimmyrivers; 07-02-2013 at 09:33 AM. Reason: Adding File

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Excel Permutations

    You could use nested for loops to try all the possible permutations:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-02-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Excel Permutations

    Thank you very much! It worked! I kept trying to think of a way to do it without nested for statements but this is so much simpler. I ended up with the following code: (I don't know how to add the code tags like you did)

    Sub permTry()
    'define all variables
    Dim c1 As Integer, c2 As Integer, c3 As Integer
    Dim c4 As Integer, c5 As Integer, c6 As Integer
    Dim c7 As Integer, c8 As Integer, c9 As Integer
    Dim c10 As Integer, c11 As Integer, c12 As Integer

    'reset all cells to 1 (probably not needed but just in case
    Worksheets("Output 5").Cells(28, 2).Value = 1
    Worksheets("Output 5").Cells(28, 3).Value = 1
    Worksheets("Output 5").Cells(28, 4).Value = 1
    Worksheets("Output 5").Cells(28, 5).Value = 1
    Worksheets("Output 5").Cells(28, 6).Value = 1
    Worksheets("Output 5").Cells(28, 7).Value = 1
    Worksheets("Output 5").Cells(28, 8).Value = 1
    Worksheets("Output 5").Cells(28, 9).Value = 1
    Worksheets("Output 5").Cells(28, 10).Value = 1
    Worksheets("Output 5").Cells(28, 11).Value = 1
    Worksheets("Output 5").Cells(28, 12).Value = 1
    Worksheets("Output 5").Cells(28, 13).Value = 1


    'loop through permutations untill a solution is found
    For c1 = 1 To 3
    Worksheets("Output 5").Cells(28, 2).Value = c1
    For c2 = 1 To 3
    Worksheets("Output 5").Cells(28, 3).Value = c2
    For c3 = 1 To 3
    Worksheets("Output 5").Cells(28, 4).Value = c3
    For c4 = 1 To 3
    Worksheets("Output 5").Cells(28, 5).Value = c4
    For c5 = 1 To 3
    Worksheets("Output 5").Cells(28, 6).Value = c5
    For c6 = 1 To 3
    Worksheets("Output 5").Cells(28, 7).Value = c6
    For c7 = 1 To 3
    Worksheets("Output 5").Cells(28, 8).Value = c7
    For c8 = 1 To 3
    Worksheets("Output 5").Cells(28, 9).Value = c8
    For c9 = 1 To 3
    Worksheets("Output 5").Cells(28, 10).Value = c9
    For c10 = 1 To 3
    Worksheets("Output 5").Cells(28, 11).Value = c10
    For c11 = 1 To 3
    Worksheets("Output 5").Cells(28, 12).Value = c11
    For c12 = 1 To 3
    Worksheets("Output 5").Cells(28, 13).Value = c12 'code to do each time
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c12
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c11
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c10
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c9
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c8
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c7
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c6
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c5
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c4
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c3
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c2
    If Worksheets("Output 5").Cells(37, 2) = 0 Then Exit For
    Next c1

    If Worksheets("Output 5").Cells(37, 2) = 1 Then Worksheets("Output 5").Cells(26, 2).Value = "NO"

    End Sub

+ 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