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
 
The function MRAND(6,1,12) in the MOREFUNC package should do the trick here, but I can't get it to work. Has anyone used this function? I did use the CTRL-SHIFT-ENTER to enter the function in a cell, but I get a NumberError. I get the error even when using the examples in the Morefunc Help page.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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
Hi mikerickson

Thanks for the help. I will use your latest code.

Kind regards

Howard
 
Upvote 0
Help With Macro

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
No, it won't. The routine that you found forbids duplicates in the result. Your problem requires duplicates.
 
Upvote 0
try
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 '<- this line
i = UB : ii = LB  '<- this line
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 Complie Error, "Duplicate Declaration in Current Scope" . The macro stops


Private Sub VSortMA(ary, LB, UB, ref)Dim M As Variant, i As Long, ii As Long, iii As Long, ref, 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



try
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, ref, 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
In the Dim statement "ref" must be deleted and

Code:
i = UB : ii = UB
Shold be
Code:
i = UB : ii = LB

code has been edited
 
Upvote 0
Thanks for responding.

Now it stops here


Private Sub VSortMA(ary, LB, UB, ref)
Dim M As Variant, i As Long, ii As Long, iii As Long, ref, 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
Have you read my previous post carefully?

Anyway, I have edited the code already...
 
Upvote 0

Forum statistics

Threads
1,217,408
Messages
6,136,436
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