Permutations

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,572
Office Version
  1. 2021
Platform
  1. Windows
I have a series of numbers in an excel spreadsheet for eg 6, 11,7,28,23,28,45,47,41,35,38
where I want to determine the number permutations.

The criteria is as follows:

1) The maximum number number in any row/cell is 6 for eg 1, 18, 23, 38, 45, 49

2) No number must occur more than once in a cell for eg the number 11, 11 must now be allowed


It would be appreciated if you would provide me wth VBA code that will allow me to do the above

Once the Macro has been activated the data should look similar to this

11, 7, 17, 28, 23, 28
11, 7, 17, 28, 23, 45
11, 7, 17, 28, 23, 47

Howard
 
I deleted ref statement. I posted the wrong error. It stops @ VSrotMA Sub function not defined!

Private Sub VSortMA(ary, LB, UB, ref)
Dim M As Variant, i As Long, ii As Long, iii As Long, temp
i = UB: ii = LB
M = ary(Int((LB + UB) / 2), ref)
Do While ii <= i
Do While ary(ii, ref) < M
ii = ii + 1
Loop
Do While ary(i, ref) > M
i = i - 1
Loop
If ii <= i Then
For iii = LBound(ary, 2) To UBound(ary, 2)
temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
Next
ii = ii + 1: i = i - 1
End If
Loop
If LB < i Then VSrotMA ary, LB, i, ref
If ii < UB Then VSortMA ary, ii, UB, ref
End Sub
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I deleted ref statement. I posted the wrong error. It stops @ VSrotMA Sub function not defined!

You don't have any word like "VSrotMA" in the code.

Copy the code again and try
 
Upvote 0
This is the onlycode I see.


Sub test()
Dim a, i As Long, ii As Long, b(1 To 15, 1 To 7)
a = Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 2).Value
Randomize
For i = 1 To 15
For ii = 1 To UBound(a, 1): a(ii, 2) = Rnd: Next
VSortMA a, 1, UBound(a, 1), 2
b(i, 1) = "Permutations#" & i
For ii = 2 To 7: b(i, ii) = a(ii - 1, 1): Next
Next
Range("b1").Resize(15, 7).Value = b
End Sub

Private Sub VSortMA(ary, LB, UB, ref)Dim M As Variant, i As Long, ii As Long, iii As Long, temp
i = UB: ii = UB
M = ary(Int((LB + UB) / 2), ref)
Do While ii <= i
Do While ary(ii, ref) < M
ii = ii + 1
Loop
Do While ary(i, ref) > M
i = i - 1
Loop
If ii <= i Then
For iii = LBound(ary, 2) To UBound(ary, 2)
temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
Next
ii = ii + 1: i = i - 1
End If
Loop
If LB < i Then VSrotMA ary, LB, i, ref
If ii < UB Then VSortMA ary, ii, UB, ref
End Sub
 
Upvote 0
Vsort wasmisspelled. Now, I get an error at the statement in bold.

Rich (BB code):
Sub test()
Dim a, i As Long, ii As Long, b(1 To 15, 1 To 7)
a = Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 2).Value
Randomize
For i = 1 To 15
     For ii = 1 To UBound(a, 1): a(ii, 2) = Rnd: Next
     VSortMA a, 1, UBound(a, 1), 2
     b(i, 1) = "Permutations#" & i
     For ii = 2 To 7: b(i, ii) = a(ii - 1, 1): Next
Next
Range("b1").Resize(15, 7).Value = b
End Sub

Private Sub VSortMA(ary, LB, UB, ref)
Dim M As Variant, i As Long, ii As Long, iii As Long, temp
i = UB: ii = UB
M = ary(Int((LB + UB) / 2), ref)
Do While ii <= i
     Do While ary(ii, ref) < M          ii = ii + 1
     Loop
     Do While ary(i, ref) > M
          i = i - 1
     Loop
     If ii <= i Then
          For iii = LBound(ary, 2) To UBound(ary, 2)
               temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
          Next
          ii = ii + 1: i = i - 1
     End If
Loop
If LB < i Then VSortMA ary, LB, i, ref
If ii < UB Then VSortMA ary, ii, UB, ref
End Sub
 
Upvote 0
Code:
Sub test()
Dim a, i As Long, ii As Long, b(1 To 15, 1 To 7)
a = Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 2).Value
Randomize
For i = 1 To 15
     For ii = 1 To UBound(a, 1): a(ii, 2) = Rnd: Next
     VSortMA a, 1, UBound(a, 1), 2
     b(i, 1) = "Permutations#" & i
     For ii = 2 To 7: b(i, ii) = a(ii - 1, 1): Next
Next
Range("b1").Resize(15, 7).Value = b
End Sub

Private Sub VSortMA(ary, LB, UB, ref)
Dim M As Variant, i As Long, ii As Long, iii As Long, temp
i = UB: ii = LB '<- this line was wrong!
M = ary(Int((LB + UB) / 2), ref)
Do While ii <= i
     Do While ary(ii, ref) < M
          ii = ii + 1
     Loop
     Do While ary(i, ref) > M
          i = i - 1
     Loop
     If ii <= i Then
          For iii = LBound(ary, 2) To UBound(ary, 2)
               temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
          Next
          ii = ii + 1: i = i - 1
     End If
Loop
If LB < i Then VSortMA ary, LB, i, ref
If ii < UB Then VSortMA ary, ii, UB, ref
End Sub
 
Upvote 0
I get a run time error"9", subscript out of range @ Bold line



Sub test()
Dim a, i As Long, ii As Long, b(1 To 15, 1 To 7)
a = Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 2).Value
Randomize
For i = 1 To 15
For ii = 1 To UBound(a, 1): a(ii, 2) = Rnd: Next
VSortMA a, 1, UBound(a, 1), 2
b(i, 1) = "Permutations#" & i
For ii = 2 To 7: b(i, ii) = a(ii - 1, 1): Next
Next
Range("b1").Resize(15, 7).Value = b
End Sub

Private Sub VSortMA(ary, LB, UB, ref)
Dim M As Variant, i As Long, ii As Long, iii As Long, temp
i = UB: ii = LB '<- this line was wrong!
M = ary(Int((LB + UB) / 2), ref)
Do While ii <= i
Do While ary(ii, ref) < M
ii = ii + 1
Loop
Do While ary(i, ref) > M
i = i - 1
Loop
If ii <= i Then
For iii = LBound(ary, 2) To UBound(ary, 2)
temp = ary(ii, iii): ary(ii, iii) = ary(i, iii): ary(i, iii) = temp
Next
ii = ii + 1: i = i - 1
End If
Loop
If LB < i Then VSortMA ary, LB, i, ref
If ii < UB Then VSortMA ary, ii, UB, ref
End Sub
 
Upvote 0
Re: Help With Macro

This is my original post, did you not read it. Are you saying there's no way to create the combinations shown in Col's C:G, but by hand?


I found this Macro on the forum and I want to know if it will work for what I need. In Col A, I have 3 sets of numbers. I want to create all the different Combinations or Permutation with only 5 in a row, likethe ones in Col's C to G. I know very little Excel and nothing about Macros. Thanks for all suggestions.
PERMUTATIONS.xls
ABCDEFGHI
1300300300300300300
2201201201201201201
3102102102102102102
4300300300300201
5
6
7
8
Sheet1







If you are selecting N items many times, this new edit will make things faster.
If you are doing truly massive numbers of selections (10,000+ choices, showing 15+ elements per choice) this will run into memory issues and the current one is better (after numChoicesMade is dimensioned Long). But if you are below that number, this speeds things up in the 200 - 10,000 choice range.

Code:
Rem - - - - - - begin New Edit - - - - - - - - - - -

Rem where to start writing
    Set writeRay = ThisWorkbook.Sheets(1).Range("c2")
    Set writeRay = Range(writeRay, _
                    writeRay.Cells(numChoicesMade, elementsPerChoice))

Dim outputRRay As Variant
ReDim outputRRay(1 To numChoicesMade, 1 To elementsPerChoice)

Application.Calculation = xlManual
Application.ScreenUpdating = False

Rem permutation  routine
    For j = 1 To numChoicesMade
    
    Rem randomly re-order selectFrom
        maxIndex = UBound(selectFrom) + 1
        For i = 1 To elementsPerChoice
            randIndex = i + Int(Rnd() * (maxIndex - i))
            
            temp = selectFrom(randIndex)
            selectFrom(randIndex) = selectFrom(i)
            selectFrom(i) = temp
            
            outputRRay(j, i) = selectFrom(i)
        Next i
        
    Next j
    writeRay.Value = outputRRay
    
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic

End Sub
 
Upvote 0

Forum statistics

Threads
1,217,404
Messages
6,136,419
Members
450,011
Latest member
faviles5566

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top