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
 
And which 15 out of 0.3 millions possibilities? How is Ravi supposed to decide that?
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi
I have written a macro where I used random numbers to shuffle your list. But I notice many repeat numbers. Programs are available on the net to generate permutations. I could not figure out how to use them. I am planning to modify this macro so that it picks up only the list of non-repeats. Hope to be ready by sunday
Ravi
 
Upvote 0
Well done Ravi ... I hope that's what the OP wants.
 
Upvote 0
HI
List your numbers from A1: A12

paste the following codes in the macro window ( alt F8)

Sub rrr()
For e = 3 To 17
For a = 3 To 8
c = Int((12 * Rnd) + 1)
Cells(1, a) = Cells(c, 1)
Next a
For b = 3 To 8
Cells(1, 10) = "=countif(A3:A8,A" & b & ")"
If Cells(1, 10) > 1 Then
Else
Cells(e, 2) = "Permutations # " & e - 2
For d = 3 To 8
Cells(e, d) = Cells(1, d)
Next d
End If
Next b
Next e
Range("C1:J1").ClearContents
End Sub

run the macro.
you wil get 15 permutations of 12 numbers six at a time.

Ravi
 
Upvote 0
Hi Ravi

Thanks for the code. I entered 12 numbers in cells A1:A12. 15 permutations were extracted containing 6 numbers each. Some of the rows contained duplicate numbers i.e amongst the 6 numbers there were duplicate numbers for eg
43 38 38 23 23 44

The numbers per row of 6 numbers must be uniqie.

I have attached the file after running the macro and have highlighted the duplicate numbers. Kindly amend your Macro so that the 6 numbers er row are unique.

Kind Regards

Howard
Permutations.Macro.xls
ABCDEFGHI
11
211
314Permutations#1433838232344
423Permutations#21444443128
528Permutations#347442848471
631Permutations#448283844140
738Permutations#5312340402323
840Permutations#6444440484714
943Permutations#7434814381148
1044Permutations#843138111144
1147Permutations#923123282348
1248Permutations#10482823111140
13Permutations#11282843234014
14Permutations#1214401314723
15Permutations#13442823484040
16Permutations#14311138434747
17Permutations#1513847314338
18
Sheet1
 
Upvote 0
Put as many values as you like in column A.
Run this macro and a random selection of those values (without duplicates) will be shown.
(caveat: if there are duplicates in column A, that might be refelected in dupliates in the permiutations shown.)
Code:
Sub permiutation()
Dim selectFrom As Variant, i As Integer, howMany As Integer, thisIndex As Integer
Dim numOfPerm, k As Integer
Dim xRay As Range, xCell As Range
Set xRay = Range(ActiveSheet.Range("a1"), ActiveSheet.Range("a65536").End(xlUp))

numOfPerm = Application.InputBox("how many permiutations do you want to see?", Type:=1)
If numOfPerm = False Then Exit Sub
numOfPerm = Int(numOfPerm)
If numOfPerm < 1 Then Exit Sub

howMany = Application.InputBox("how many elements in each permiutation?", Type:=1)
If howMany = False Then Exit Sub
howMany = Int(howMany)
If howMany < 1 Then Exit Sub

Range(Cells(1, 2), Cells(1, i + numOfPerm)).EntireColumn.ClearContents
For k = 1 To numOfPerm
    selectFrom = Application.Transpose(xRay)
    ReDim Preserve selectFrom(1 To UBound(selectFrom) + 1)


    For i = 1 To howMany
    
        thisIndex = Int(Rnd() * UBound(selectFrom)) + 1
        Cells(i, 1 + k).Value = selectFrom(thisIndex)
        For j = thisIndex To (UBound(selectFrom) - 1)
            selectFrom(j) = selectFrom(j + 1)
        Next j
        ReDim Preserve selectFrom(1 To UBound(selectFrom) - 1)
    
    Next i
Next k
End Sub
 
Upvote 0
Permuatation.1.xls
ABCDEFGHIJKLMNOP
11441483847444323311447311
21138474428234411282843281140
314404428384338144031234412343
423144344443814381123384431
528231143114481112814434044
631483114028114848441128434738
738
840
943
1044
1147
1248
Sheet1
 
Upvote 0
permutations

Hi mikerickson

Thanks for your code. It is working almost perfectly. I entered 12 numbers into cells A1:A12 and activated the macro. The number of permutations required in this case was 15, containing 6 numbers.

15 permutations were extracted. In most instances 6 unique numbers were extracted, in some cases 5 numbers and one blank cell.

I have attached my file so that you can see what was extracted.

It would be appreciated if you would amend your code so that unique numbers are extracted for the number of elements required in this eg 6 elements were required


Your assistance will be most appreciated.

Regards

Howard
 
Upvote 0
I think this is better than my last suggestion.
This will write the permiutations in rows, beginining at C2 of ThisWorkbook.Sheets(1).

Code:
Option Explicit

Sub testTwo()
Dim i As Integer, j As Integer
Dim temp As Variant
Dim deflt As String
Static elementsPerChoice As Integer
Static numChoicesMade As Integer
Static elementsRay As Range

Dim selectFrom As Variant
Dim randIndex As Integer, maxIndex As Integer
Dim writeRay As Range

Rem get elements
    Rem input range
        If Not (elementsRay Is Nothing) Then _
        deflt = elementsRay.Address
        On Error Resume Next
        Set elementsRay = Application.InputBox _
                            ("Select the elements to permute.", _
                                Default:=deflt, _
                                Type:=8)
        If TypeName(elementsRay) <> "Range" Then Exit Sub
        On Error GoTo 0
        If Not (elementsRay.Rows.Count = 1 _
            Or elementsRay.Columns.Count = 1) _
            Then Beep: Exit Sub
    
    Rem range to array
        If elementsRay.Columns.Count = 1 Then
            selectFrom = Application.Transpose(elementsRay)
        Else
            Rem array handeling for my environment
            selectFrom = Application.Transpose(Application.Transpose(elementsRay))
        End If

Rem how many elements per choice
    If elementsPerChoice = 0 Then elementsPerChoice = 6
    deflt = CStr(elementsPerChoice)
    elementsPerChoice = Application.InputBox( _
                                "How many elements in each choice?", _
                                Default:=deflt, Type:=1)
    If elementsPerChoice = False Then Exit Sub
    If elementsPerChoice > elementsRay.Cells.Count Then _
                    elementsPerChoice = elementsRay.Cells.Count

Rem - - - - - - -Begin Edit  - - - - - - - - - - - - - - -

Rem input how many to show and length of permiutation
    If numChoicesMade = 0 Then numChoicesMade = 15
    If elementsPerChoice = 0 Then elementsPerChoice = 6
    deflt = CStr(numChoicesMade) & ", " & CStr(elementsPerChoice)

    Do
        temp = Application.InputBox( _
                    prompt:="Enter the number of choices to display and" _
                    & vbCrLf & vbTab & "the number of elements in each choice," _
                    & vbCrLf & vbTab & vbTab & "separated by a comma." _
                    & vbCrLf & vbCrLf & vbTab & "rows_displayed , elements_per_row", _
                    Default:=deflt, _
                    Type:=2)
        If temp = "False" Then Exit Sub
        On Error Resume Next
        j = Application.Search(",", temp, 2)
        On Error GoTo 0
    Loop Until j > 0

    numChoicesMade = Val(temp)
    elementsPerChoice = Val(Mid(temp, j + 1))
    If elementsPerChoice > elementsRay.Cells.Count Then _
        elementsPerChoice = elementsRay.Cells.Count

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

Rem - - - - - - begin New Edit - see post below - - - - - - -

Application.Calculation = xlManual
    
Rem - - - - - - - - - -End Edit - - - - - - - - - - - - - - - - 
Rem main routine
    For j = 1 To numChoicesMade
    
        Rem randomly reorder selectFrom
        maxIndex = UBound(selectFrom)
        For i = 1 To elementsPerChoice
            randIndex = Int(Rnd() * maxIndex) + 1
            temp = selectFrom(randIndex)
            selectFrom(randIndex) = selectFrom(i)
            selectFrom(i) = temp
            Rem maxIndex = maxIndex - 1 (not needed)
        Next i
        
        Rem show the first n elements of that re-ordering
        writeRay.Offset(j - 1, 0).Value = selectFrom
    
    Next j
End Sub
 
Upvote 0
Hi
I am surprised at your output. I used to get it like that before I introduced countif function to check for duplicates. Now I have repeatedly checked but don't find any duplicates. I suggest you copy the macro and run it again. If you still have problems, I will fix it.
Ravi
 
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